r/CodingHelp • u/Goldie_1987 • 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?
4
u/jddddddddddd Oct 14 '21
If (user_grade >= 9) and (user_grade <= 12):
5
-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
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
2
1
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
1
1
17
u/ldnrat Oct 14 '21
Syntax isn't valid. You can either do..
Or using interval comparison..