这是数学测验不起作用。我不知道为什么

时间:2018-08-30 15:57:33

标签: math python-3.6

import random

while True:
    calc_1 = (random.randint(1,50)) #generates random variables
    calc_2 = (random.randint(1,50))
    print (calc_1,"+",calc_2)       #prints the random question
    a = ((calc_1)+(calc_2))         #calculates the random question
    q = input ("? ")
    if q == a :
        print ("right")
        break
    else:
        print ("wrong")

答案正确时,它不会说正确。我已经测试了其他几种可能性,但我无法弄清楚。

1 个答案:

答案 0 :(得分:0)

input() 可为您提供str,以便在比较之前将其转换为int

import random

while True:
    calc_1 = (random.randint(1, 50))  # generates random variables
    calc_2 = (random.randint(1, 50))
    print(calc_1, "+", calc_2)  # prints the random question
    a = ((calc_1) + (calc_2))  # calculates the random question
    q = input("? ")
    try:
        q = int(q)
        if q == a:
            print("right")
            break
        else:
            print("wrong")
    except:
        print('Not a number')