0

我有一个针对儿童的 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!")
每当我测试它并单击“是”时,程序都会关闭。

4

3 回答 3

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}那么代码将在方法内部运行。

于 2014-11-19T00:49:26.427 回答
0
while (playOnBool):
    playOn = easygui.buttonbox ("Do you want to play again?", choices = ["Yes", "No"])
    if playOn == "Yes": playOnBool = True
    else: playOnBool = False

你需要用一个while循环来包装你的代码。

于 2014-11-19T00:44:53.627 回答
0

最后的代码不在顶部的“while”循环中。

由于 Python 采用缩进方式,因此程序将在最后设置 playOn 变量后结束。

我假设中间必须有代码,至少是“通过”,否则 Python 会给出缩进块错误。

于 2014-11-19T00:45:37.037 回答