0

can you tell me whats wrong with this please? I keep getting SyntaxErrors and im not really to sure whats going on becasue im new to the language and have been given very little information from my school work booklet.

score = int(input("What Score did you get?"))

if score <= 39 :
    print ("You might want to try a bit harder, you got a grade U.")
if score =>40 and <=49:
    print("Good, but you could try a little harder next time. You got a grade D")
if score =>50 and <= 59:
    print ("Good Job! You got a grade C")
if score =>60 and <= 69:
    print("Great! You got a grade B")
if score =>70:
    print ("Excellent! Thats a grade A!")
user3142463
  • 11
  • 1
  • 4

3 Answers3

3

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!")
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
2

You are missing three score(s):

if score ==40 and score <=49:

instead of

if score ==40 and <=49:

Final Code:

score = int(input("What Score did you get?"))
if score <= 39 :
    print ("You might want to try a bit harder, you got a grade U.")
if score ==40 and score<=49:
    print("Good, but you could try a little harder next time. You got a grade D")
if score >=50 and score<= 59:
    print ("Good Job! You got a grade C")
if score >=60 and score <= 69:
    print("Great! You got a grade B")
if score >=70:
    print ("Excellent! Thats a grade A!")
Devashish Das
  • 241
  • 7
  • 19
1

Your Syntax errors are happening here.

if score ==40 and <=49:
    print("Good, but you could try a little harder next time. You got a grade D")
if score =>50 and <= 59:
    print ("Good Job! You got a grade C")
if score =>60 and <= 69:
    print("Great! You got a grade B")

Python's and statement combines 2 boolean statements, in your case:

score =>60

<= 69

while the first one is valid, python does not recognize the second. I am assuming you are comparing the score variable in both cases so you can fix it by adding score next to the second boolean statement like so:

if score ==40 and score <=49:
    print("Good, but you could try a little harder next time. You got a grade D")
if score =>50 and score <= 59:
    print ("Good Job! You got a grade C")
if score =>60 and score <= 69:
    print("Great! You got a grade B")
wcb98
  • 172
  • 1
  • 1
  • 10