Python if分支执行后未按预期终止 实现游戏流程控制与重启循环
问题根源
你的分支判断只处理了「选right时打印失败提示」的逻辑,没有做流程终止或跳转,所以分支代码执行完后,程序会按顺序继续往下运行,自然就弹出了step2的输入提示。
可选解决方案
方案1:选错直接终止程序
最简单的改法,在right分支打印完失败提示后,加一行exit()直接终止程序运行即可。另外你原来判断right的时候没加.lower(),用户输入大写的Right/RIGHT会匹配失败,顺手给你补上。
修改后的step1段代码:
step1 = input(" Do you pick left or right? ") if step1.lower() == "left": print("congratulations you have avoid the pit of snakes! and moved to your next choice!") elif step1.lower() == "right": print(""" OH no you fell into a pit full of SNAKES! --..,_ _,.--. `'.'. .'`__ o `;__. '.'. .'.'` '---'` ` '.`'--....--'`.' `'--....--'` """) exit() # 执行到这里直接终止程序,不会继续往下跑step2 step2 = input("You come up to a lake do you wait or do you swim? ")
方案2:失败后支持重启游戏
要实现重开逻辑,给整个游戏套一层外层循环即可,失败后询问用户意愿,选重开就回到游戏初始流程,选退出就终止程序。
参考实现结构:
while True: # 游戏开场逻辑 print(''' ******************************************************************************* | | | | _________|________________.=""_;=.______________|_____________________|_______ | | ,-"_,="" `"=.| | |___________________|__"=._o`"-._ `"=.______________|___________________ | `"=._o`"=._ _`"=._ | _________|_____________________:=._o "=._."_.-="'"=.__________________|_______ | | __.--" , ; `"=._o." ,-"""-._ ". | |___________________|_._" ,. .` ` `` , `"-._"-._ ". '__|___________________ | |o`"=._` , "` `; .". , "-._"-._; ; | _________|___________| ;`-.o`"=._; ." ` '`."\` . "-._ /_______________|_______ | | |o; `"-.o`"=._`` '` " ,__.--o; | |___________________|_| ; (#) `-.o `"=.`_.--"_o.-; ;___|___________________ ____/______/______/___|o;._ " `".o|o_.--" ;o;____/______/______/____ /______/______/______/_"=._o--._ ; | ; ; ;/______/______/______/_ ____/______/______/______/__"=._o--._ ;o|o; _._;o;____/______/______/____ /______/______/______/______/____"=._o._; | ;_.--"o.--"_/______/______/______/_ ____/______/______/______/______/_____"=.o|o_.--""___/______/______/______/____ /______/______/______/______/______/______/______/______/______/______/_____ / ******************************************************************************* ''') print("Welcome to Treasure Island.") print("Your mission is to find the treasure.") name = input("What is your Name ? ") print(f"Welcome {name} to the Treasure Island game. Lets get started!") game_over = False step1 = input(" Do you pick left or right? ") if step1.lower() == "left": print("congratulations you have avoid the pit of snakes! and moved to your next choice!") elif step1.lower() == "right": print(""" OH no you fell into a pit full of SNAKES! --..,_ _,.--. `'.'. .'`__ o `;__. '.'. .'.'` '---'` ` '.`'--....--'`.' `'--....--'` """) game_over = True # 失败后走重启询问逻辑 if game_over: restart = input("Game Over! 输入y重新开始游戏,按其他键退出:") if restart.lower() == "y": continue # 回到循环开头,重开游戏 else: break # 跳出循环,结束程序 # 后续关卡逻辑写在这里 step2 = input("You come up to a lake do you wait or do you swim? ") # 后续step2、step3等所有失败分支,只要把game_over设为True即可,会自动走上面的重启询问流程
后续开发提示
你之后写其他关卡的失败分支时,要么直接调用exit()终止程序,要么标记game_over = True走重启逻辑,不要让执行流自然落到后续关卡的代码上,就不会再出现选错选项还能继续推进剧情的问题。
内容的提问来源于stack exchange,提问作者Saqib Munir
相关产品推荐
相关产品推荐

