Jump to content

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


TroyL99

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 comment
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 comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...