为什么答案正确时我的数学测验总是打印不正确 [英] why does my math quiz always print incorrect when the answer is correct

查看:90
本文介绍了为什么答案正确时我的数学测验总是打印不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这篇关于为什么答案正确时我的数学测验总是打印不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆