总是说错误的答案?

时间:2014-12-02 21:59:08

标签: python random

我正在做一个简单的数学测验它的工作正常,除非我问它是否正确的答案总是说不正确,如果它的正确与否。我不知道我哪里出错了任何帮助都会受到赞赏。

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

1 个答案:

答案 0 :(得分:1)

变量PLAYERA将是一个字符串。变量maths将是一个整数。在Python中,"7"7不同,因此您的if语句永远不会成立。

因此,您需要:

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

请注意,此代码会导致错误,即播放器的输入不是数字。你可以通过这样做来避免这种情况:

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