默认情况下,
当我运行程序时,我会输入正确的答案,但它总是将其标记为错误:
(10,'+',2)
12
…
答错了,答案是12!
这是错误的代码部分:
Player_answer = input()
print ("...")
time.sleep (0.5)
if operation==("+"): #check answer
answer = num1+num2 #This works out the real answer
if Player_answer == answer: #This works out if the player is correct
print("That's the correct answer")
score = score + 1
else:
print("Wrong answer, the answer was",answer,"!")
if operation==("*"):
answer = num1*num2
if Player_answer == answer:
print("That's the correct answer")
score = score + 1
else:
print("Wrong answer, the answer was",answer,"!")
elif operation==("-"):
answer = num1-num2
if Player_answer == answer:
print("That's the correct answer")
score = score + 1
else:
print("Wrong answer, the answer was",answer,"!")
input()
返回一个字符串。因此,即使您键入12,Player_input
也是"12"
(一个字符串)。要使Player_input
成为int
eger,只需键入Player_input = int(Player_input)
。int()
函数接受任何数据值,并尝试将其转换为整数。但是,如果它无法转换,例如尝试转换"not num"
,它将引发一个ValueError
。为了防止这种情况,您可以将其封装在try/except
语句中。