user6067123
user6067123

Reputation:

Fixing maths quiz in python

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")

Upvotes: 2

Views: 344

Answers (2)

wenxi
wenxi

Reputation: 133

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

Upvotes: 0

armatita
armatita

Reputation: 13485

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 == '+':

Upvotes: 5

Related Questions