问题描述
我正在尝试做一个算术测验,但遇到了这个问题:即使我输入了正确答案,它似乎也忽略了正确答案代码并直接转到了错误答案代码.基本上,它不接受任何正确答案.
I am attempting to make an arithmetic quiz, but have run into this issue: Even if I input the correct answer, it seems to ignore the correct answer code and go straight to the incorrect answer code. Basically, it doesn't accept any right answers.
import random
num1 = (random.randrange(10))
num2 = (random.randrange(10))
correct1 = (num1*num2)
ans1 = input("What is " + str(num1) + " multiplied by " + str(num2) + "? ")
if ans1 == correct1:
print("Correct! ")
if ans1 != correct1:
print(" Incorrect. ")
print(" The correct answer was " + str(ans1))
运行时,我得到如下信息:
When ran, I get something like this:
What is 3 multiplied by 0? 0
Incorrect.
The correct answer was 0
注意答案和我的输入是如何相同的,但它运行了错误答案的代码.谁能帮我解决这个问题?我使用的是 Python 3.4.
Note how the answer and my input were the same, but it ran the code for an incorrect answer. Can anyone help me to fix this? I am using Python 3.4.
推荐答案
3
不等于 "3"
.调用 input
(在 Python3 中)的结果是一个字符串,而不是一个数字.
3
is not equal to "3"
. The result of a call to input
(in Python3) is a string, not a number.
在用户输入上调用 int
...
ans1 = input("What is " + str(num1) + " multiplied by " + str(num2) + "? ")
ans1 = int(ans1)
...
这篇关于Arthimatic 测验不接受正确答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!