如何使python程序循环/重复

时间:2014-11-19 00:42:15

标签: python easygui

我有一个儿童的python编程拼写游戏,如果玩家完成游戏后点击是,我需要让它循环/重启,如果他们点击否则退出程序。

这是我编程的首要任务。

#Declare Constants and Variables
Score = 0
PlayerAnswer = 0
playOn = 0
while playOn != "Yes":

这就是结束,我希望玩家能够在easygui按钮框上点击“是”时重复游戏。

playOn = easygui.buttonbox ("Do you want to play again?", choices = ["Yes", "No"])
if playOn == "Yes":
    Score = 0 #resets score count, if player wants to play again
    
elif playOn == "No":
        easygui.msgbox ("Bye for now. Hope you'll play the game again soon!")
无论何时我测试它并单击是,程序都会关闭。

3 个答案:

答案 0 :(得分:0)

while (playOnBool):
    playOn = easygui.buttonbox ("Do you want to play again?", choices = ["Yes", "No"])
    if playOn == "Yes": playOnBool = True
    else: playOnBool = False

您需要使用while循环包装代码。

答案 1 :(得分:0)

最后的代码不在'而'循环在顶部。

由于Python经过缩进,程序将在最后设置playOn变量后结束。

我认为中间必须有代码,至少是'传递'否则Python会给出缩进的块错误。

答案 2 :(得分:0)

while playOn != "Yes":
   playOn = easygui.buttonbox ("Do you want to play again?", choices = ["Yes", "No"])
   if playOn == "Yes":
      Score = 0 #resets score count, if player wants to play again

   elif playOn == "No":
     easygui.msgbox ("Bye for now. Hope you'll play the game again soon!")

在Python中,代码体需要缩进,以便将其解释为代码块内部。 在其他语言(如C#)中,只要代码在method{ //code inside here}内,代码就会在方法内部运行。