简体   繁体   English

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

[英]python question for random math question generator

i cant make the list for the result of the answer that is typed in. i wanted to make a list so it shows all the result i typed in but the list is not showing.我无法为输入的答案的结果制作列表。我想制作一个列表,以便显示我输入的所有结果,但列表未显示。 can anyone help me with the list and make my code working?谁能帮我列出清单并使我的代码正常工作? this is the code这是代码

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)

you typed " answer_that_is_typed " instead " answerthatistyped "您输入了“ answer_that_is_typed ”而不是“ answerthatistyped

essentially i think there is a typo, maybe you meant list_result instead of listresult .基本上我认为有一个错字,也许你的意思是list_result而不是listresult However as you are using python i'd like to suggest you a more "pythonic" way to reach your solution:但是,当您使用 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