import random
questions = 1
score = 0
input("Please enter your name: ")
operator = ["+","-","*"]
while questions<10:
num1 = (random.randint(0,15))
num2 = (random.randint(0,15))
picked_operator = random.choice(operator)
print("What is " + str(num1) +str(picked_operator) +str(num2), "?")
question = '{} {} {}'.format(num1, picked_operator, num2)
answer = input()
if answer == eval(question):
print("You are correct")
score = score+1
else:
print("incorrect")
questions = questions + 1
print(answer)打印的例子:请输入你的名字:丹,什么是11 + 3? 14不正确的14,什么是13-7?.
发布于 2022-02-07 09:19:36
错误与您的input()语句有关。用这个代替:
answer = int(input())
# OR
if answer == str(eval(question)):
# Do things您接受answer作为字符串数量,并将其与整数进行比较,请参见下面的内容:
>>> 12 == '12'
False发布于 2022-02-07 09:21:27
Python中的eval函数返回一个Integer,而输入函数返回一个字符串。当您将Int与Str进行比较时,您将无法得到相等的结果。
answer = int(input())
应该是那样的好
https://stackoverflow.com/questions/71016013
复制相似问题