我的输入怎么不等于答案?
How does my input not equal the answer?
从 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())
是的,"5"
与 5
不同,您完全正确。您可以使用 str(5)
将 5
转换为字符串。另一种方法是通过 int("5")
将 "5"
转换为整数,但该选项可能会失败,因此最好处理异常。
因此,对您的程序所做的更改可能是例如以下:
if guess == str(a):
而不是:
if guess == a:
另一种选择是将猜测值转换为整数,如其他答案中所述。
编辑:这仅适用于 Python 版本 2.x:
但是,您使用的是 input()
,而不是 raw_input()
。 input()
returns 如果您键入一个整数(如果您键入的文本不是有效的 Python 表达式,则会失败)。我测试了你的程序,它问 What is 4 - 2?
;我输入了 2
,结果显示 Correct!
,所以我看不出你的问题是什么。
您是否注意到,如果您的程序询问 What is 9 - 4?
,您可以键入 9 - 4
,它会显示 Correct!
?那是因为您使用的是 input()
,而不是 raw_input()
。同样,如果您输入例如c
,您的程序失败 NameError
不过,我会使用 raw_input()
,然后将答案与 str(correct_answer)
进行比较
我假设您使用的是 python3。
您的代码的唯一问题是您从 input()
获得的值是一个字符串而不是整数。所以你需要转换它。
string_input = input('Question?')
try:
integer_input = int(string_input)
except ValueError:
print('Please enter a valid number')
现在您的输入是一个整数,您可以将其与 a
进行比较
编辑代码:
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):
# you get the user input, it will be a string. eg: "5"
guess = input("")
# now you need to get the integer
# the user can input everything but we cant convert everything to an integer so we use a try/except
try:
integer_input = int(guess)
except ValueError:
# if the user input was "this is a text" it would not be a valid number so the exception part is executed
print('Please enter a valid number')
# if the code in a function comes to a return it will end the function
return
if integer_input == a:
print("Correct!")
else:
print("Wrong, the answer is",a)
askQuestion(generateQuestion())
从 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())
是的,"5"
与 5
不同,您完全正确。您可以使用 str(5)
将 5
转换为字符串。另一种方法是通过 int("5")
将 "5"
转换为整数,但该选项可能会失败,因此最好处理异常。
因此,对您的程序所做的更改可能是例如以下:
if guess == str(a):
而不是:
if guess == a:
另一种选择是将猜测值转换为整数,如其他答案中所述。
编辑:这仅适用于 Python 版本 2.x:
但是,您使用的是 input()
,而不是 raw_input()
。 input()
returns 如果您键入一个整数(如果您键入的文本不是有效的 Python 表达式,则会失败)。我测试了你的程序,它问 What is 4 - 2?
;我输入了 2
,结果显示 Correct!
,所以我看不出你的问题是什么。
您是否注意到,如果您的程序询问 What is 9 - 4?
,您可以键入 9 - 4
,它会显示 Correct!
?那是因为您使用的是 input()
,而不是 raw_input()
。同样,如果您输入例如c
,您的程序失败 NameError
不过,我会使用 raw_input()
,然后将答案与 str(correct_answer)
我假设您使用的是 python3。
您的代码的唯一问题是您从 input()
获得的值是一个字符串而不是整数。所以你需要转换它。
string_input = input('Question?')
try:
integer_input = int(string_input)
except ValueError:
print('Please enter a valid number')
现在您的输入是一个整数,您可以将其与 a
编辑代码:
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):
# you get the user input, it will be a string. eg: "5"
guess = input("")
# now you need to get the integer
# the user can input everything but we cant convert everything to an integer so we use a try/except
try:
integer_input = int(guess)
except ValueError:
# if the user input was "this is a text" it would not be a valid number so the exception part is executed
print('Please enter a valid number')
# if the code in a function comes to a return it will end the function
return
if integer_input == a:
print("Correct!")
else:
print("Wrong, the answer is",a)
askQuestion(generateQuestion())