因此,基本上,我有一个测验程序,最后,我想让用户能够说出是否想再次玩,如果说是,它将带回到顶部并重新开始,但是相反,我的编码正在做的是跳过要重新播放的问题,让他们回答我编码顶部的前两个问题,然后问他们是否要再次播放,但问题循环不断直到我说不]
这是我的编码的第一个函数和主要语句部分,由于太长而无法放入整件事中
def main():
atawhai, student_id = details()
quiz_type = quiz()
complete_quiz(quiz_type)
if quiz_type == QUIZ_CHOICE[0]:
correct_answers = g_responses()
else:
correct_answers = r_responses()
display(atawhai, student_id, correct_answers)
play_again = "Y"
while play_again == "Y":
atawhai, student_id = details()
play_again = input("Do you want to retake the quiz Y or N")
main()
每次游戏结束时,您都需要将代码包装在while循环中,并不断询问用户。例如,您可以将游戏逻辑保留在名为play的函数中,在主函数中,将play again标志的默认值设置为y或Y并在while循环中执行代码,并在循环结束时询问用户是否想要再次播放,循环将再执行一次,依此类推...检查下面的代码
def play():
# you game logic goes here ...
print("my game code is here")
def main():
play_again = "y"
while play_again == "y" or play_again == "Y":
play()
play_again = input("Do you want to play again (y,n)?")
print("Bye!")
谢谢