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

1

4 Answers

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:

  • is 9 smaller or equal to user_grade AND
  • is 9 smaller or equal to 12

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.

10

Your condition is wrong. You should use:

if 9 <= user_grade <= 12:
    print('in high school')

The order of the comparsion matters. user_grade >= 9 <= 12 means user grade is inferior or equals to 9 AND 9 is inferior or equals to 12.

Guess that it should more readable as follows:

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

The answer to the zybooks question is:

8 < user_grade < 13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest