【发布时间】:2025-12-03 21:10:01
【问题描述】:
首先说我对 python 完全陌生,我必须做一个学校项目,我被这部分代码困住了。几乎,通过 IF 语句的要求得到满足(用户输入等于答案,我敢肯定)并且系统直接跳到 ELSE。我不确定我是否只是失明,这是一个简单的格式问题,还是我遗漏了其他东西。
while valid == False:
topic = input().lower()
if topic == "addition":
print ("You have selected Addition!")
valid = True
ARand1 = randrange(1, 200)
ARand2 = randrange(1, 200)
question_answer = ARand1 + ARand2
print ("Question",questions_asked,":")
print ("What is",ARand1,"+",ARand2,"?")
users_answer = input("Please input your answer")
if users_answer == (question_answer):
print ("Correct!")
total_score += 1
questions_asked += 1
else:
print ("Incorrect!")
questions_asked += 1
print (questions_asked)
elif topic == "subtraction":
print ('You have selected Subtraction!')
valid = True
SRand1 = randrange(100, 200)
SRand2 = randrange(1, 100)
answer = SRand1 - SRand2
print ("Question",questions_asked,":")
print ("What is",SRand1,"-",SRand2,"?")
我遇到困难的部分是第二个 if 语句(如果 users_answer == (question_answer):)。即使条件满足,它也会完全跳过 IF 并直接进入 ELSE。
抱歉,如果它是简单的东西或者写得不好,我几周前才刚刚被介绍给 Python。
【问题讨论】:
-
users_answer始终是一个字符串...question_answer是一个整数。它们不是同一类型,所以不相等
标签: python python-2.7