Search code examples
pythonpython-3.3except

Invalid syntax for except ValueError


When i run this code (Python 3.3):

counter=0
score=0
import random
a = random.randint(1,25)
b = random.randint(1,25)
c=a+b
try:
    answer=int(input("What is "+str(a)+" + "+str(b)+" ? ")                
    if answer == c:
        score=score+1
        print("Well done!")
        print("Your score is "+str(score)+".")
        print("")
        counter = counter + 1
    else:
        print("Thats wrong. The correct asnwer is: "+str(c)+".")
        print("Your score is "+str(score)+".")
        print("")
        counter = counter + 1
except ValueError:
       print("sadas")

I says "invalid syntax" and it highlight the colon from "if answer == c:" red.


Solution

  • You are missing a closing parenthesis:

    answer=int(input("What is "+str(a)+" + "+str(b)+" ? ")                
    #         ^     ^                                    ^?
    #         |      \----------------------------------/ |
    #          \-----------------------------------------/