为什么答案正确时我的数学测验总是打印不正确

编程入门 行业动态 更新时间:2024-10-24 12:25:04
本文介绍了为什么答案正确时我的数学测验总是打印不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

好的,所以我正在编写一个代码,该代码随机生成问题并让用户回答,但是我的问题是,即使用户正确地回答了,它也会始终打印不正确

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")

更多推荐

为什么答案正确时我的数学测验总是打印不正确

本文发布于:2023-11-30 04:06:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648659.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不正确   测验   正确   答案   数学

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!