总是说答案不对?

2024-06-08 13:35:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个简单的数学测验,它运行得很好,除非我问它是不是正确的答案,它总是说不正确,如果它是正确的。我不知道我哪里出了问题。如果你能帮上忙,我会很感激的。在

import random
QuestN = 1
QNC = 0
CS = 0
WS = 0

while True:
    #Number Of Questions:
    QNC = QNC + 1
    if QNC == 10:
        break

    #Choosing The Questions
    ops = ['+', '-', '*', '/']
    num1 = random.randint(0,12)
    num2 = random.randint(1,10)
    operation = random.choice(ops)

    #Simplifying The Operations
    if operation == "+":
        NEWOP = "+"
    elif operation == "-":
        NEWOP = "-"
    elif operation == "*":
        NEWOP = "x"
    elif operation == "/":
        NEWOP = "÷"

    #Asking The Questions
    print("Awnser This:")
    print(num1)
    print(NEWOP)
    print(num2)
    maths = eval(str(num1) + operation + str(num2))

    #Awnsering The Questions
    PLAYERA = input(": ")
    print(maths)
    #Keeping Score
    if PLAYERA == maths:
        print("Correct")
        CS = CS +1 
    else:
        print("Incorrect")
        WS = WS +1
    print()

#RESTART

Tags: theifwsrandomcsoperationopsquestions
1条回答
网友
1楼 · 发布于 2024-06-08 13:35:33

变量PLAYERA将是一个字符串。变量maths将是一个整数。在Python中,"7"7不是一回事,所以你的if语句永远不会是真的。在

因此,您需要:

if int(PLAYERA) == maths:
    print("Correct")
    CS = CS +1 

请注意,如果玩家输入的不是数字,则此代码将导致错误。您可以通过这样做来避免这种情况:

^{pr2}$

相关问题 更多 >