如何根据玩家当前房间从字典中仅打印对应单个条目
问题:根据当前房间仅打印对应房间信息
我希望根据玩家当前所在房间位置,每次仅打印字典中对应的一组键值对,但目前尝试的所有方法要么打印全部条目,要么重复打印(次数等于条目总数),要么无任何输出。
我的房间字典代码
rooms = { 'Lower Deck': {'up': 'Main Deck'}, 'Main Deck': {'down': 'Lower Deck', 'forward': 'Main Hallway', 'item': 'Pistol'}, 'Main Hallway': {'right': 'Crew Cabins', 'back': 'Main Deck', 'forward': 'Air Lock'}, 'Crew Cabins': {'back': 'Main Hallway', 'item': 'Dog tags'}, 'Air Lock': {'back': 'Main Hallway', 'forward': 'Crews Lounge'}, 'Crews Lounge': {'back': 'Air Lock', 'right': 'Escape pods', 'left': 'Engineering Room', 'up': 'Operation Room', 'item': 'Grenade'}, 'Escape pods': {'back': 'Crews Lounge'}, 'Engineering Room': {'back': 'Crews Lounge'}, 'Operation Room': {'back': 'Crews Lounge', 'forward': 'Control Center'}, 'Control Center': {'back': 'Operation Room'} }
我尝试的代码(效果不好)
for i in rooms: i = current_room print('The current exit to the room are: {}'.format(rooms[i]))
输出结果(重复打印当前房间内容,次数等于字典条目数):
The current exit to the room are: {'up': 'Main Deck'} The current exit to the room are: {'up': 'Main Deck'} The current exit to the room are: {'up': 'Main Deck'} ...
可正常运行的房间移动代码
current_room = 'Lower Deck' command = input("Please enter your move:\n").split() if command[0].capitalize() == 'Go': if command[1] in directions: room_dict = rooms[current_room] if command[1] in room_dict: current_room = room_dict[command[1]] else: print("There is no exit that direction.\n")
解决方案
1. 核心问题:多余的for循环
你这段代码重复打印的原因是遍历了整个rooms字典的所有键,每次循环又把i强制赋值为current_room,所以字典有多少个房间,就会重复打印多少次当前房间的信息。直接去掉for循环,通过current_room直接取值即可。
2. 基础版:仅打印当前房间信息
不需要循环,一行代码就能搞定:
print(f'当前房间的出口信息:{rooms[current_room]}')
这样只会打印一次当前房间对应的键值对,不会重复。
3. 优化版:友好的信息展示
如果觉得直接打印字典不够直观,可以把出口和物品分开展示,提升体验:
current_room_data = rooms[current_room] # 提取所有出口(排除item键) exits = [direction for direction in current_room_data if direction != 'item'] print(f'\n你现在在:{current_room}') print(f'可前往的方向:{", ".join(exits)}') # 检查房间内是否有物品 if 'item' in current_room_data: print(f'房间里有:{current_room_data["item"]}')
4. 整合到游戏循环中
把展示房间信息的逻辑封装成函数,放到移动完成后调用,实现动态更新:
rooms = { 'Lower Deck': {'up': 'Main Deck'}, 'Main Deck': {'down': 'Lower Deck', 'forward': 'Main Hallway', 'item': 'Pistol'}, 'Main Hallway': {'right': 'Crew Cabins', 'back': 'Main Deck', 'forward': 'Air Lock'}, 'Crew Cabins': {'back': 'Main Hallway', 'item': 'Dog tags'}, 'Air Lock': {'back': 'Main Hallway', 'forward': 'Crews Lounge'}, 'Crews Lounge': {'back': 'Air Lock', 'right': 'Escape pods', 'left': 'Engineering Room', 'up': 'Operation Room', 'item': 'Grenade'}, 'Escape pods': {'back': 'Crews Lounge'}, 'Engineering Room': {'back': 'Crews Lounge'}, 'Operation Room': {'back': 'Crews Lounge', 'forward': 'Control Center'}, 'Control Center': {'back': 'Operation Room'} } current_room = 'Lower Deck' directions = ['up', 'down', 'forward', 'back', 'left', 'right'] def show_current_room(): """展示当前房间的信息""" current_room_data = rooms[current_room] exits = [d for d in current_room_data if d != 'item'] print(f'\n=== 当前位置:{current_room} ===') print(f'出口方向:{", ".join(exits)}') if 'item' in current_room_data: print(f'发现物品:{current_room_data["item"]}') # 游戏开始时先显示初始房间 show_current_room() # 主游戏循环 while True: command = input("\n请输入指令(格式:Go + 方向,或Quit退出):\n").strip().split() if not command: continue cmd_head = command[0].capitalize() if cmd_head == 'Go': if len(command) < 2: print("请输入完整指令,例如:Go up") continue direction = command[1].lower() if direction in directions: if direction in rooms[current_room]: current_room = rooms[current_room][direction] show_current_room() else: print("那个方向没有出口!") else: print("无效方向,请输入:up/down/forward/back/left/right") elif cmd_head == 'Quit': print("游戏结束,再见!") break else: print("未知指令,请重新输入!")
内容的提问来源于stack exchange,提问作者Joe
相关产品推荐
相关产品推荐

