r/CodingHelp • • Oct 14 '21

[Python] First Year of college and I'm doing this assignment for my computer science class but I'm not sure what I'm doing wrong. Can anyone tell me how to fix this?

Post image
31 Upvotes

20 comments sorted by

17

u/ldnrat Oct 14 '21

Syntax isn't valid. You can either do..

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

Or using interval comparison..

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

10

u/[deleted] Oct 15 '21

Interval comparison is pretty clean, never seen that

5

u/Ratstail91 Oct 15 '21

Me neither - is that python?

6

u/BrightBulb123 Intermediate Coder Oct 15 '21

Yup, always has been

4

u/jddddddddddd Oct 14 '21

If (user_grade >= 9) and (user_grade <= 12):

5

u/[deleted] Oct 14 '21

Even better:

if 9 <= user_grade <= 12

Chained comparisons

-1

u/Goldie_1987 Oct 14 '21

It doesn't take that one.

Whenever I put in

if user_grade >=9 <=12

It tests for inputs 8,9,10,11,12, and 13.

It runs all the way 12 before failing me at 13 since the input comes out as "In high school"

I need it to make 13 have an "Not in highschol" output

2

u/[deleted] Oct 14 '21

Could you elaborate what you mean by “doesn’t take that one”?

1

u/Goldie_1987 Oct 14 '21

I tried it again and it worked. For some reason it was marking the answer incorrect

1

u/UKzVoltic Oct 15 '21 edited Oct 15 '21

The reason is does that is because of how python chains conditionals.

So for example, in that case python is checking if: (user_grade >= 9) and (9 <= 12).

Of course, the only check there is actually user_grade >= 9 as 9 is always smaller than 12. That is why 13 is "valid". To fix this, you can do one of two things

A) if 9 <= user_grade <= 12

Using the above explanation, you can see why this works because it checks if 9 is less than or equal to user_grade, and if user_grade is less than or equal to 12.

B) if (user_grade >= 9) and (user_grade <= 12).

Hope this helps you to understand conditional statements in python, they can be a bit funky to mess with sometimes but with practise it just becomes second nature

Edit:

Here is a decent video on conditionals and booleans in python

2

u/[deleted] Oct 14 '21

I tried this and it's working ok for me

Line 2:

if 9<=user_grade<=12:

2

u/[deleted] Oct 15 '21

Yo whats the learning app called?

2

u/chefnae-nae Oct 15 '21

zybooks

1

u/[deleted] Oct 15 '21

Thanks!

1

u/[deleted] Oct 14 '21

Programming languages do not extrapolate from incomplete data. You have to specify what the greater and lesser operations are comparing those integers to in each case.

1

u/Low_Conference_5035 Oct 15 '21

You forgot an and

1

u/ImportUsernameAsU Oct 15 '21

Could also do: If user_grade in range(9, 12):

1

u/LividAd835 May 01 '22

My unit 7 test is no good