2

MY maths quiz was working but now. It says after 'if ops =='+':' that the colon is an invalid syntax. Please can you help me fix it. If I remove the colon below it the answer variable would come up as an invalid syntax error.

import random

question=0
userInput = int()
LastName = str()
answer = str()
FirstName = str()
Form = str()

def Quiz():
    score=0
    LastName = input ("Please enter your surname: ").title()
    FirstName = input ("Please enter your first name: ").title()
    Form = input ("Please enter your form: ").title()





for i in range(10):
    print ("What is:")
    num1= random.randint(1,12)
    num2= random.randint(1,12)
    ops = ['+', '-', '*']
    operation = random.choice(ops)
    Q = int(input(str(num1)+operation+str(num2) 

    if ops =='+':
            answer==num1+num2
            if Q == answer:
                    print ("correct")
                    score= score+1

            else:
                    print('You Fail')


    elif ops =='-':
            answer==num1-num2
            if Q == answer:
                    print ("correct")
                score= score+1
            else:
                     print("you fail")

    else:
            answer==num1*num2
            if Q == answer:
                    print ("correct")
                    score= score+1
            else:
                    print("you fail")
  • I guess you wanted to write "if operation == '+'" instead of "if ops==" – Marco Mar 21 '16 at 09:52
  • it comes up with same error if I do operation instead of ops –  Mar 21 '16 at 09:53
  • 1
    I'm no python programmer but do you really mean `answer==num1+num2`and not `answer = num1+num2` - double `=`vs. single `=`? – mnille Mar 21 '16 at 09:54
  • it comes up with the colon as a invaded syntax error even with only one equal symbol. –  Mar 21 '16 at 09:55

2 Answers2

5

You are missing two parenthesis on this line:

 Q = int(input(str(num1)+operation+str(num2)))

,This will lead into a syntax error in the next line.

ALSO:

Remove the double = in lines like this:

 answer=num1+num2  #it was == before

Declare "score" before starting the game:

 score = 0

Replace "ops" with "operation":

 if operation == '+':
armatita
  • 12,825
  • 8
  • 48
  • 49
0

Use if operation=='+'. ops is a list, you want to match char (operation).

wenxi
  • 123
  • 6