在python中修复数学测验

时间:2016-03-21 09:49:57

标签: python

我的数学测验现在正在运作。它说'如果ops ==' +':'冒号是无效的语法。请你帮我解决一下。如果我删除它下面的冒号,则答案变量会出现无效的语法错误。

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

2 个答案:

答案 0 :(得分:5)

您在此行中缺少两个括号:

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

,这将导致下一行语法错误。

ALSO:

删除double = in这样的行:

 answer=num1+num2  #it was == before

声明"得分"在开始游戏之前:

 score = 0

替换" ops"使用"操作":

 if operation == '+':

答案 1 :(得分:0)

使用if operation=='+'ops是一个列表,您希望匹配char(operation)。