提问人:Zeina 提问时间:5/26/2023 最后编辑:OneCricketeerZeina 更新时间:5/26/2023 访问量:86
在基于文本的冒险游戏中停止 Python while 循环
Stopping a Python while loop in a text-based adventure game
问:
我正在用 python 语言编写一个基于文本的冒险游戏,不幸的是我无法停止游戏。我试着把代码写--->
def GAME_RESTART():
i = input("would you like to play again?(y/n)")
if i == "y":
Game()
elif i == "n":
print_pause("YOU'VE COME TO AN END!")
print_pause("Hopefully the game finds you well")
while i != "y" and i != "n":
print_pause("Incorrect input. Please choose (y/n)") *#print_pause is a function I defined already in the rest of the code*
pass
GAME_RESTART()
有什么建议如何破解代码以停止工作吗?
我实际上尝试使用命令break,但它什么也没做
答:
0赞
Dan Nagle
5/26/2023
#1
重构代码以利用循环。函数无需调用自身。while
def GAME_RESTART():
while True:
i = input("would you like to play again?(y/n)")
if i == "y":
Game()
elif i == "n":
print_pause("YOU'VE COME TO AN END!")
print_pause("Hopefully the game finds you well")
break # exit the loop
else:
print("Incorrect input. Please choose (y/n)")
评论
0赞
Zeina
5/28/2023
实际上,感谢大家的帮助,我仍然是 python 的初学者,但我使用了 quit() 函数来结束游戏
上一个:无法结束循环
评论
break
return
GAME_RESTART()
打电话真的没有意义GAME_RESTART()
Game
GAME_RESTART