user8739422
user8739422

Reputation:

Simple python if/else syntax not working

I am trying to create a simple test-scorer that grades your test and gives you a response - but a simple if/else function isn't running -

Python -

testScore = input("Please enter your test score")


if testScore <= 50:
  print "You didn't pass... sorry!" 
elif testScore >=60 and <=71:
  print "You passed, but you can do better!"

The Error is -

Traceback (most recent call last):
  File "python", line 6
    elif testScore >= 60 and <= 71:
                              ^
SyntaxError: invalid syntax

Upvotes: 1

Views: 11730

Answers (3)

Rafsan
Rafsan

Reputation: 64

You made some mistakes here:

  • You are comparing a string with an Integer if testScore <= 50:

  • You have missed the variable here --> elif testScore >=60 and <=71:

I think those should be like this --->

  • if int(testScore) <= 50:
  • elif testScore >=60 and testScore<=71:

And try this, it is working --->

testScore = input("Please enter your test score")
if int(testScore) <= 50:
    print ("You didn't pass... sorry!") 
elif testScore >=60 and testScore<=71:
    print ("You passed, but you can do better!")

Upvotes: 0

Chetan_Vasudevan
Chetan_Vasudevan

Reputation: 2414

The below shown way would be the better way of solving it, you always need to make the type conversion to integer when you are comparing/checking with numbers.

input() in python would generally take as string

 testScore = input("Please enter your test score")
 if int(testScore) <= 50:
     print("You didn't pass... sorry!" )
 elif int(testScore) >=60 and int(testScore)<=71:
     print("You passed, but you can do better!")

Upvotes: 3

Sandeep Lade
Sandeep Lade

Reputation: 1943

You missed testScore in elif statement

 testScore = input("Please enter your test score")


if testScore <= 50:
  print "You didn't pass... sorry!" 
elif testScore >=60 and testScore<=71:
  print "You passed, but you can do better!"

Upvotes: 6

Related Questions