我正在做一个简单的数学测验,它运行得很好,除非我问它是不是正确的答案,它总是说不正确,如果它是正确的。我不知道我哪里出了问题。如果你能帮上忙,我会很感激的。在
import random
QuestN = 1
QNC = 0
CS = 0
WS = 0
while True:
#Number Of Questions:
QNC = QNC + 1
if QNC == 10:
break
#Choosing The Questions
ops = ['+', '-', '*', '/']
num1 = random.randint(0,12)
num2 = random.randint(1,10)
operation = random.choice(ops)
#Simplifying The Operations
if operation == "+":
NEWOP = "+"
elif operation == "-":
NEWOP = "-"
elif operation == "*":
NEWOP = "x"
elif operation == "/":
NEWOP = "÷"
#Asking The Questions
print("Awnser This:")
print(num1)
print(NEWOP)
print(num2)
maths = eval(str(num1) + operation + str(num2))
#Awnsering The Questions
PLAYERA = input(": ")
print(maths)
#Keeping Score
if PLAYERA == maths:
print("Correct")
CS = CS +1
else:
print("Incorrect")
WS = WS +1
print()
#RESTART
变量
PLAYERA
将是一个字符串。变量maths
将是一个整数。在Python中,"7"
与7
不是一回事,所以你的if
语句永远不会是真的。在因此,您需要:
请注意,如果玩家输入的不是数字,则此代码将导致错误。您可以通过这样做来避免这种情况:
^{pr2}$相关问题 更多 >
编程相关推荐