Python房间移动文字游戏循环逻辑Bug排查修复
问题根因
循环逻辑异常、需要两次输入才更新状态,由以下明确错误导致:
- 第一次输入的指令被直接丢弃:进入内层循环后未处理循环外读取的第一次输入,立刻重新调用
input()读取新内容,第一次输入完全不生效 - 房间配置字典存在错误:
Flowerhaus对应的方向值写成了集合{"RIGHT", "Barn"},不是方向到房间的键值对映射,正确格式应为{"RIGHT": "Barn"}Candy Store的UP方向写为"the outside",和初始房间名"the outdoors"拼写不一致,移动时会触发KeyError
- 循环退出条件完全失效:外层循环判断
direction != exit,其中exit是Python内置函数对象,和字符串类型的用户输入永远不相等,外层循环永远不会正常退出 - QUIT指令判断位置错误:写在
direction in avail_directions的elif分支中,只有输入无效方向时才会识别退出指令,正常输入时无法触发退出 - 道具拾取逻辑有缺陷:会重复拾取已拿到的钥匙,
None值也会被拼接到背包字符串中触发类型错误,且字符串拼接无分隔符,道具名会堆成乱码 - 双层循环结构冗余:文本冒险游戏用单层主循环即可实现,嵌套循环反而会提升逻辑分支的复杂度,引发不可预期的问题
修复后完整代码
spaces = { "the outdoors": {"UP": "Barn", "RIGHT": "Magistrate's Home", "DOWN": "Candy Store"}, "Flowerhaus": {"RIGHT": "Barn"}, "Barn": {"LEFT": "Flowerhaus", "RIGHT": "Random Cabin", "DOWN": "the outdoors"}, "Magistrate's Home": {"UP": "Random Cabin", "DOWN": "Workshop", "LEFT": "the outdoors"}, "Workshop": {"UP": "Magistrate's Home", "LEFT": "Candy Store"}, "Candy Store": {"UP": "the outdoors", "LEFT": "Tavern", "RIGHT": "Workshop", "DOWN": "Papermill"}, "Tavern": {"RIGHT": "Candy Store", "DOWN": "Empty House"}, "Empty House": {"UP": "Tavern", "RIGHT": "Papermill"}, "Papermill": {"UP": "Candy Store", "LEFT": "Empty House"}, "Random Cabin": {"DOWN": "Magistrate's Home", "LEFT": "Barn"}, } items = {"Flowerhaus": None, "Tavern": None, "Barn": "Car Key", "Random Cabin": "Neighbor's Key", "Magistrate's Home": "Deadbolt Key", "Workshop": "Office Key", "Empty House": "House Key", "Papermill": "Mailbox Key", "Candy Store": "Ex's Key"} current_space = "the outdoors" current_inventory = [] avail_directions = tuple(spaces[current_space].keys()) # 打印开场剧情 print("It's been a long, long, frustrating day.") print("You've entered the front door to your home, ready to take your shoes off and relax...") print("As you walk through the door, you realize this is not your house, your city, or even your realm of existence.") print("Warily, you step forward to further take in your surroundings.") print("The floor is dirt, the buildings around you look strange, and the people don't seem to react to your presence.") print("") print("After a short time, you realize your keys are no longer in your hand.") print("As you turn back around, you notice the door is still there, but now it has seven locks, each appearing") print("to match the locks that go to all seven of your keys.") print("Turning the knob proves the portal home is locked.") print("Find all seven keys to unlock the door. Fail to find all seven keys and the door will disappear,") print("leaving you trapped forever.") print("") print("So, with all of that in mind, where would you like to look first?") # 单层主循环,移除冗余嵌套 while True: inv_display = ", ".join(current_inventory) if current_inventory else "a whole lot of nothing" print(f"You are currently in {current_space}, with {inv_display} on your keyring.") direction = input("Type UP, DOWN, LEFT, RIGHT to move, or QUIT to exit: ").upper() # 优先判断退出指令 if direction == "QUIT": print("Oh... So, you're going to take a break then? Yeah, maybe a nap is a good idea. Try again later.") break # 校验方向合法性 if direction not in avail_directions: print("Nothing over here. Don't you want to get back home? Try a different direction.") continue # 执行移动 current_space = spaces[current_space][direction] avail_directions = tuple(spaces[current_space].keys()) print(f"You walk through the door and enter {current_space}.") # 拾取道具:仅当房间存在未拾取的道具时触发 room_item = items.get(current_space) if room_item and room_item not in current_inventory: current_inventory.append(room_item) print(f"You found the {room_item}.")
修复说明
- 移除冗余双层循环,改用单层主循环,逻辑路径清晰,不会出现输入丢失问题
- 修正房间字典中的集合格式错误、房间名拼写错误
- 背包改用列表存储,展示时自动添加分隔符,同时增加重复拾取判断,不会重复获取同一道具,也不会将None值加入背包触发报错
- 调整指令判断顺序,优先识别QUIT指令,输入后立刻正常退出
- 调整状态打印和输入读取的顺序,第一次输入的指令会被正常处理,无需重复输入两次
内容的提问来源于stack exchange,提问作者TheeJynx
相关产品推荐
相关产品推荐

