繁体   English   中英

随机数学问题生成器的 python 问题

[英]python question for random math question generator

我无法为输入的答案的结果制作列表。我想制作一个列表,以便显示我输入的所有结果,但列表未显示。 谁能帮我列出清单并使我的代码正常工作? 这是代码

answerthatistyped = []

def asknumber():
    while (1):
        try:
            answer = int(input("enter the correct number"))
            break
        except ValueError:
            print("incorrect input")
            continue
        return answer
def askquestion():
    number_1 = random.randint(1,5)
    number_2 = random.randint(1,5)

    print("what is" ,number_1,"+",number_2,)

    result = asknumber()
    list_result = int(result) - 0
    answer_that_is_typed.append(listresult)
    if result == number_1 + number_2:
        return 0
    else:
        return 1

amount = 6
correct = 0
for i in range(amount):
    correct += askquestion()

print("You got %d correct out of %d" % (correct, amount))
print(answerthatistyped)```
answerthatistyped.append(listresult)

您输入了“ answer_that_is_typed ”而不是“ answerthatistyped

基本上我认为有一个错字,也许你的意思是list_result而不是listresult 但是,当您使用 python 时,我想建议您采用更“pythonic”的方式来解决您的问题:

answerthatistyped = []

def asknumber():
    while True:
        try:
            answer = int(input("enter the correct number"))
            break
        except ValueError:
            print("incorrect input")
            continue
        return answer
def askquestion():
    number_1 = random.randint(1,5)
    number_2 = random.randint(1,5)

    print(f"what is {number_1} + {number_2}")

    result = asknumber()
    answer_that_is_typed.append(int(result))
    return not bool(result == number_1 + number_2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM