【发布时间】:2015-05-26 04:34:00
【问题描述】:
暂时从 Unity JS 切换到 Python,我无法理解为什么这不起作用。
我最好的猜测是变量guess实际上是一个字符串,所以字符串5和整数5不一样?
这是正在发生的事情吗?无论怎样解决这个问题。
import random
import operator
ops = {
'+':operator.add,
'-':operator.sub
}
def generateQuestion():
x = random.randint(1, 10)
y = random.randint(1, 10)
op = random.choice(list(ops.keys()))
a = ops.get(op)(x,y)
print("What is {} {} {}?\n".format(x, op, y))
return a
def askQuestion(a):
guess = input("")
if guess == a:
print("Correct!")
else:
print("Wrong, the answer is",a)
askQuestion(generateQuestion())
【问题讨论】: