我使用Python 3进行测验程序。我试图执行检查,以便如果用户输入字符串,控制台不会吐出错误。我输入的代码不起作用,我不知道如何去修复它。如何检查用户是否输入了号码?
import random
import operator
operation=[
(operator.add, "+"),
(operator.mul, "*"),
(operator.sub, "-")
]
num_of_q=10
score=0
name=input("What is your name? ")
class_num =input("Which class are you in? ")
print(name,", welcome to this maths test!")
for _ in range(num_of_q):
num1=random.randint(0,10)
num2=random.randint(1,10)
op,symbol=random.choice(operation)
print("What is",num1,symbol,num2,"?")
if int(input()) == op(num1, num2):
print("Correct")
score += 1
try:
val = int(input())
except ValueError:
print("That's not a number!")
else:
print("Incorrect")
if num_of_q==10:
print(name,"you got",score,"/",num_of_q)
“不行”如何? – TigerhawkT3
什么不适用于它? – Academiphile
如果我输入一个字符串,无论如何都会出现错误,如果我输入正确的答案,那么问题就会停止,我必须按Enter才能获得下一个问题。如果我得到答案错误没有任何反应。 – CyberSkull311