Python is not English; and
will not guess that you are testing against the same variable. You'll have to spell it out instead:
if score >= 40 and score <= 49:
or you can use comparison chaining:
if 40 <= score <= 49:
where Python essentially will do the same thing (it means the same as (40 <= score) and (score <= 49)
but you don't have to repeat the score
variable or use and
anymore).
However, because only ever one test will match if you use a combination of if
.. elif
.. else
branches, you could test for higher values instead, and only need to test for one boundary in each branch:
if score >= 70:
print("Excellent! Thats a grade A!")
elif score >= 60:
print("Great! You got a grade B")
elif score >= 50:
print("Good Job! You got a grade C")
elif score >= 40:
print("Good, but you could try a little harder next time. You got a grade D")
else:
print("You might want to try a bit harder, you got a grade U.")
Note the difference between this and your separate series of if
statements; those are not related to one another, while using elif
and else
very much is.
So if the score is 70 or up, the first test matches and all the tests are skipped. If the score is merely 55, say, then the first two tests don't match, but the third one does, so the remainder is skipped.
If you preferred your ordering, then test for the upper bounds instead of the lower:
if score < 40:
print("You might want to try a bit harder, you got a grade U.")
elif score < 50:
print("Good, but you could try a little harder next time. You got a grade D")
elif score < 60:
print("Good Job! You got a grade C")
elif score < 70:
print("Great! You got a grade B")
else:
print("Excellent! Thats a grade A!")