Jump to content

Nested if code doesn't make any sense...


Recommended Posts

The following code reason doesn't make sense.

If x = 41 and I check if its greater than 10 and 20, why would I print out "but not above 20." x is greater than 20 and it's greater than 30 and 40 as well. 

Perhaps w3schools could use a better example for beginners to make it more clear. 

Thank you!

Nested If

You can have if statements inside if statements, this is called nested if statements.

Example

x = 41

if x > 10:
  print("Above ten,")
  if x > 20:
    print("and also above 20!")
  else:
    print("but not above 20.")

 

Link to post
Share on other sites
  • 2 weeks later...

Hey Troy! Welcome to the Forums! This example is supposed to work for most values of x.

Given that x is 41, the only output would be

Above ten,
and also above 20!

And if you changed x to be... for instance 16, the output would be

Above ten,
but not above 20.

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...