本文介绍了为什么答案正确时我的数学测验总是打印不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好的,所以我正在编写一个代码,该代码随机生成问题并让用户回答,但是我的问题是,即使用户正确地回答了,它也会始终打印不正确
okay so im writing a code that randomly generates questions and lets the user answer but my problem is that even if the user gets the answer right it will always print incorrect
print ("what is your username") name = input () .title() print (name, "welcome") import random score=0 question=0 for i in range(10): ops = ["+", "-", "*"] num1 = random.randint (0,10) num2 = random.randint (0,10) oparator = random.choice(ops) Q=(str(num1)+(oparator)+(str(num2))) print (Q) guess = input() guess = int(guess) if oparator =='+': answer = (str(num1+num2)) elif oparator =='-': answer = (str(num1-num2)) else: oparator =='*' answer = (str(num1*num2)) if guess == (Q): print ("correct") score + 1 else: print ("incorrect")老实说,我不明白哪里出了问题. 任何帮助将不胜感激 ps我知道我的密码很乱
I honestly don't understand what is wrong. any help would be greatly thanked p.s I know my codes a mess
推荐答案您需要将猜测与答案进行比较.
You need to compare guess with answer.
print ("what is your username") name = input().title() print (name, "welcome") import random score=0 question=0 for i in range(10): ops = ["+", "-", "*"] num1 = random.randint (0,10) num2 = random.randint (0,10) oparator = random.choice(ops) Q=(str(num1)+(oparator)+(str(num2))) print (Q) guess = input() guess = int(guess) if oparator =='+': answer = int(str(num1+num2)) # Convert to int elif oparator =='-': answer = int(str(num1-num2)) else: oparator =='*' answer = int(str(num1*num2)) if guess == answer: # Compare user's answer with actual answer print ("correct") score = score + 1 # Update the score else: print ("incorrect")更多推荐
为什么答案正确时我的数学测验总是打印不正确
发布评论