我正在制作一个用户应该编写答案的游戏(question()函数)。在问题函数中,如果用户的答案错误或正确,我将使用变量1和0来获取信息。我还使用变量1和0来查看用户是否回答了问题。
def main():
menu()
anv_val = ber_val()
val(anv_val)
def menu():
print("1. game")
print("2. stat")
print("3. end")
def ber_val():
val = input("Your choice: ")
while val not in ["1", "2", "3"]:
print("Print 1, 2 or 3.")
val = input("Your choice: ")
return val
def val(anv_val):
if (anv_val == "1"):
res = game()
return res
elif (anv_val == "2"):
res = game()
return stat(res)
else:
return end()
def question(quest, solu):
print(quest)
answer = input("Your answer: ")
a_s = 1
a_f = 0
while (answer != solu):
a_f = 1
print("Try again")
answer = input("Your answer: ")
print("Correct!")
a_f_s = [a_f, a_s]
return a_f_s
def game():
a_1 = question("Your name?", "Ricky")
a_2 = question("Your name?", "Rong")
a_3 = question("Your name?", "Bolly")
sum_f = a_1[0] + a_2[0] + a_3[0]
sum_s = a_1[1] + a_2[1] + a_3[1]
sum_all = [sum_f, sum_s]
return sum_all, main()
def stat(res): # Here I get the error message
print("Questions you answered: " + str(res[1]))
print("Wrong times: " + str(res[0]))
return main()
main()
现在是我的问题。在函数game()中,我想返回名为a_f_s的变量,并在函数stat()中使用该信息(该信息是错误和已回答问题的数量)。但是问题是在函数val(anv_val)中-如果用户打印“ 2”,则函数game()将运行,但是我希望stat()运行。我有点困惑。
感谢所有帮助!