pythonif-statementchaining

Why is this basic if-else expression failing?


This is my first post on the site. I am in my third week of coding class and have completed everything but this problem. We are using ZyBooks. I have completed everything but this participation exercise. It isn't graded; however, it is driving me nuts. We are asked to write an expression that will print "in high school" if the value of user_grade is between 9 and 12 (inclusive).

Sample output with input: 10 in high school

This is what I have so far: I apologize if I am posting this incorrectly.

user_grade = int(input())
if user_grade >= 9 <= 12:
    print('in high school')
else:
    print('not in high school')

The code passes all tests until it runs input: 13. Then I receive this error:

Output differs. See highlights below.

Special character legend Your output: in high school

Expected output: not in high school


Solution

  • I think your order of operations in the if is slightly wrong. Do you mean the following:

    if  9 <= user_grade <= 12:
    

    Because right now you are checking:

    If you enter 13 then the first check will be True. But as the second check is only comparing 9 and 12 it is always true. And therefore you are never testing if user_input is smaller or equal to 12.