gpt4 book ai didi

python - 将 python 中的 while 循环条件重置为 "play the quiz again"

转载 作者:太空宇宙 更新时间:2023-11-04 10:19:10 25 4
gpt4 key购买 nike

我还是新手。本质上,我只需要重置 while 循环的代码。

当用户完成测验时,它会询问用户是否想再玩一次。如果用户输入 y,测验应该重新开始。

我的代码:

print("Moose Quiz")

question = 0 # Tells us which questions the user has completed
questions = ["What is the average life span of a moose?: ",
"How much do moose eat on a daily basis?: ",
"The fastest moose ran...?: "]

userAnswers = ["","",""] # Stores the users answers

answers = ["a. 10 - 14 years\nb. 15 - 25 years\nc. Blue\nd. 26 - 35 years\n",
"a. 24 lbs a day\nb. 39 lbs a day\nc. 67 lbs a day\nd. 73 lbs a day",
"a. 20 mph\nb. 25 mph\nc. 35 mph\nd. 40 mph"]

correct = 0

while question < 3:
print(questions[question])
print(answers[question])
answers[question] = input("To answer, pick a letter or leave it blank to skip it: ").lower()

if question == 0:
if answers[question] == "a":
print()
print("Sorry, please try again.")
question = question + 1
print()
elif answers[question] == "b":
print()
print("Good Job! That is correct.")
correct = correct + 1
question = question + 1
print()
elif answers[question] == "c":
print()
print("Sorry, please try again.")
question = question + 1
print()
elif answers[question] == "d":
print()
print("Sorry, please try again.")
question = question + 1
print()
elif answers[question] == "":
print("Awww...you skipped one!")
question = question + 1
print()
else:
print("Invalid character, please try again.")

elif question == 1:
if answers[question] == "a":
print()
print("Sorry, please try again.")
question = question + 1
print()
elif answers[question] == "b":
print()
print("Sorry, please try again.")
question = question + 1
print()
elif answers[question] == "c":
print()
print("Sorry, please try again.")
question = question + 1
print()
elif answers[question] == "d":
print()
print("Terrific! You got it right!")
correct = correct + 1
question = question + 1
print()
elif answers[question] == "":
print("Awww...you skipped one!")
question = question + 1
print()
else:
print("Invalid character, please try again.")

elif question == 2:
if answers[question] == "a":
print()
print("Sorry, please try again.")
question = question + 1
print()
elif answers[question] == "b":
print()
print("Sorry, please try again.")
question = question + 1
print()
elif answers[question] == "c":
print()
print("Amazing! You're awesome!")
correct = correct + 1
question = question + 1
print()
elif answers[question] == "d":
print()
print("Sorry, please try again.")
question = question + 1
print()
elif answers[question] == "":
print("Awww...you skipped one!")
question = question + 1
print()
print("Thanks for playing!")
again = input("Would you like to play again?: ")

最佳答案

最后,您可以将问题重置为0

while question < 3:
...

again = input("Would you like to play again?: ")
if again == 'y':
question = 0

关于python - 将 python 中的 while 循环条件重置为 "play the quiz again",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33447809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com