如何检查用户的输入是否正确并输出正确的输出?

时间:2019-09-13 01:43:57

标签: python

我正在创建一个程序,在其中输出数学问题并由用户输入答案。当我测试并输入正确的答案时,它表示这是错误的答案。正确的答案就是输入的答案。

我尝试将答案存储在变量中,但没有任何改变。我还尝试将它们放在括号中,但效果不佳。

    numberone = (randint(1, 10))
    numbertwo = (randint(1, 10))

    print(f"Sara biked at {numberone}mph for {numbertwo} hour(s). How far 
    did she travel?")
    answer= input("Enter answer here:")
    correct = (numberone*numbertwo)
    if answer == correct:
      print("Correct!")
    else:
      print(f"Sorry that is the wrong answer. The correct answer is 
      {correct}.")

我输入了numberone * numbertwo,但是它说我错了。

1 个答案:

答案 0 :(得分:2)

您正在将一个整数与input()中的值进行比较,该值返回一个字符串。更改要包装在int()函数中的输入行。

numberone = (randint(1, 10))
numbertwo = (randint(1, 10))

print(f"Sara biked at {numberone}mph for {numbertwo} hour(s). How far 
did she travel?")
answer= int(input("Enter answer here:")) # wrap the input in int() to compare a number not a string
correct = (numberone*numbertwo)
if answer == correct:
  print("Correct!")
else:
  print(f"Sorry that is the wrong answer. The correct answer is 
  {correct}.")
相关问题