Python乘法测验语法错误

时间:2015-03-04 07:32:05

标签: python syntax syntax-error multiplication

在编写一个程序时,总体上为用户创建了10个乘法问题的测验,我遇到了一个语法错误,我在修复时没有成功。当我尝试运行代码时,有一个语法错误,指向“if userAns == numAns:”之后的冒号。这是我的代码:

import random
numOne = 0
numTwo = 0
numRight = 0
numProblems = 0
numAns = 0
userAns = 0

while numProblems < 10:
    numProblems+=1
    numOne = random.randint(0,100)
    numTwo = random.randint(0,100)
    numAnswer = numOne*numTwo
    userAnswer = int(input("What is:",numOne,"*",numTwo,"?")
    if userAns == numAns:
        numRight += 1
if numRight >0<3:
    print("That was pretty bad.. ")
elif numRight >3 and numRight<=6:
    print("Hey not bad..")
elif numRight >6 and numRight <=9:
    print("You're pretty good....")
elif numRight == 10:
    print("OK OK...You're smarter than me...")
else:
    print("ERROR")

感谢您提供的任何帮助

2 个答案:

答案 0 :(得分:1)

userAnswer = ...行上的几个问题:

  • 最后遗漏括号
  • input需要一个参数

为了解决这些问题,请尝试以下方法:

userAnswer = int(input("What is: {} * {}? ".format(numOne, numTwo)))

答案 1 :(得分:0)

此行需要右括号。

userAnswer = int(input("What is:",numOne,"*",numTwo,"?"))

你最好像这样写一下提示信息:

...
while numProblems < 10:
    numProblems+=1
    numOne = random.randint(0,100)
    numTwo = random.randint(0,100)
    numAnswer = numOne*numTwo
    hint = "What is:" + str(numOne) + "*" + str(numTwo) + "?"
    userAnswer = int(input(hint))

    if userAns == numAns:
        numRight += 1
...