Python文本类游戏全局变量赋值报错求助:local variable referenced before assignment
解决Python文本游戏中全局变量user_input的赋值问题
错误原因
在forestscene函数里,你对user_input执行了赋值操作(user_input = input("> ")),Python会默认把它当成局部变量。但while user_input not in commands这行代码在赋值前就引用了它,所以触发"local variable referenced before assignment"错误。你之前加global只解决了首行引用问题,但场景切换后程序结束,是因为调用roadscene()或wolfscene()后,当前函数的循环没有继续,且其他场景函数没有处理输入循环,导致执行完就退出了。
方案1:正确使用global关键字并完善循环逻辑
在函数开头声明global user_input,确保函数内操作的是全局变量,同时给每个场景函数都加上输入循环,保证切换场景后能正常交互。
修改后的代码示例:
print("story text removed for length") light = False killer_location = 2 wolves = True commands = ["n", "S", "E", "W", "north", "south", "east", "west", "look","use","pick up", "attack","jump","throw"] inventory = ["testinventoryobjectforarray"] user_input = ">" def forestscene(): global user_input # 声明使用全局变量 print("You are in a small clearing in the woods. Tall trees in every direction.") print("You see a dirt road to the north of you. ") while True: # 用无限循环维持交互 user_input = input("> ") # 先判断输入是否合法 if user_input.lower() not in [cmd.lower() for cmd in commands]: print("...") continue # 处理合法命令 if user_input.lower() in ["n", "north"]: roadscene() elif user_input.lower() in ["s", "south"]: wolfscene() elif user_input.lower() in ["e", "east"]: print("The trees are too dense and it's too dark to proceed further east.") def roadscene(): global user_input print("You're on a dirt road. The forest stretches out behind you.") while True: user_input = input("> ") if user_input.lower() in ["s", "south"]: forestscene() # 返回森林场景 break # 可添加其他道路场景的命令逻辑 def wolfscene(): global user_input print("A pack of wolves surrounds you!") while True: user_input = input("> ") if user_input.lower() in ["n", "north"]: forestscene() break elif user_input.lower() in ["attack"]: print("You fight off the wolves and return to the clearing.") forestscene() break forestscene()
方案2:用类封装游戏状态(推荐)
全局变量多了会让代码越来越难维护,用类来统一管理所有游戏状态(位置、物品、输入逻辑等),后续扩展新场景或功能会更方便。
代码示例:
class TextGame: def __init__(self): self.light = False self.killer_location = 2 self.wolves = True self.commands = ["n", "S", "E", "W", "north", "south", "east", "west", "look","use","pick up", "attack","jump","throw"] self.inventory = ["testinventoryobjectforarray"] self.current_scene = "forest" # 记录当前场景 def run(self): # 主循环,根据当前场景调用对应函数 while True: if self.current_scene == "forest": self.forestscene() elif self.current_scene == "road": self.roadscene() elif self.current_scene == "wolf": self.wolfscene() def forestscene(self): print("You are in a small clearing in the woods. Tall trees in every direction.") print("You see a dirt road to the north of you. ") user_input = input("> ") # 判断输入合法性 if user_input.lower() not in [cmd.lower() for cmd in self.commands]: print("...") return # 切换场景或执行动作 if user_input.lower() in ["n", "north"]: self.current_scene = "road" elif user_input.lower() in ["s", "south"]: self.current_scene = "wolf" elif user_input.lower() in ["e", "east"]: print("The trees are too dense and it's too dark to proceed further east.") def roadscene(self): print("You're on a dirt road. The forest stretches out behind you.") user_input = input("> ") if user_input.lower() in ["s", "south"]: self.current_scene = "forest" # 可添加更多道路场景的交互逻辑 def wolfscene(self): print("A pack of wolves surrounds you!") user_input = input("> ") if user_input.lower() in ["n", "north"]: self.current_scene = "forest" elif user_input.lower() in ["attack"]: print("You fight off the wolves and return to the clearing.") self.current_scene = "forest" if __name__ == "__main__": print("story text removed for length") game = TextGame() game.run()
内容的提问来源于stack exchange,提问作者KniteRite
相关产品推荐
相关产品推荐

