开发文本类房间收集物品游戏遇TypeError: unhashable type: 'dict'求助
文本游戏TypeError错误分析与修复
错误信息
player_move in rooms[currentRoom]: TypeError: unhashable type: 'dict'
用户代码
rooms = { 'Laundry Room': {'West': 'Basement'}, #Game Starts Here 'Basement': {'North': 'Kitchen'}, 'Kitchen': {'West': 'Living Room', 'East': 'Dining Room', 'North': 'Bedroom', 'South': 'Basement'}, 'Living Room': {'East': 'Kitchen', 'item': '$100 cash'}, 'Dining Room': {'West': 'Kitchen', 'North': 'Backyard', 'item': '$6 cash'}, 'Bedroom': {'South': 'Kitchen', 'East': 'Bathroom', 'item': '$200 cash'}, 'Bathroom': {'West': 'Bedroom', 'item': '$300 cash'}, 'Backyard': {'South': 'Dining Room', 'item': '$60 cash'} #Return to the Laundry Room via the Basement } player_move = ['North', 'South', 'East', 'West'] currentRoom = rooms['Laundry Room'] # While Loop for moving between rooms while True: player_move = input('Enter a move: \n') if player_move == 'Exit': print('Thanks for playing.') # break statement for exiting break if player_move in rooms[currentRoom]: currentRoom = rooms[currentRoom][player_move] addItem = input(' \n') if 'item' in rooms[currentRoom]: game(rooms[currentRoom]['item'])
问题根源
错误出在currentRoom的赋值和使用逻辑上:
- 初始时
currentRoom = rooms['Laundry Room'],这把currentRoom变成了一个字典(比如{'West': 'Basement'}) - 而
rooms字典的键是字符串类型的房间名(比如'Laundry Room'),当你执行rooms[currentRoom]时,相当于用一个字典当键去访问另一个字典,字典是不可哈希的类型,所以抛出TypeError: unhashable type: 'dict'
修复方案
1. 修正currentRoom的初始值
把currentRoom改成存储房间名字符串,而不是房间字典:
currentRoom = 'Laundry Room' # 存房间名,不是房间字典
2. 调整后续代码的访问逻辑
所有需要获取当前房间信息的地方,用rooms[currentRoom]来访问对应的字典:
while True: player_move = input('Enter a move: \n') if player_move == 'Exit': print('Thanks for playing.') break # 检查当前房间是否有该方向的出口 if player_move in rooms[currentRoom]: # 更新currentRoom为目标房间的名字 currentRoom = rooms[currentRoom][player_move] # 补充物品收集的交互提示 addItem = input('是否收集当前房间的物品?(Y/N)\n') # 检查当前房间是否有物品 if 'item' in rooms[currentRoom]: # 替换未定义的game函数,补充实际收集逻辑 print(f"你收集到了{rooms[currentRoom]['item']}") # 可移除物品避免重复收集 # del rooms[currentRoom]['item']
3. 其他小问题修复
- 变量名冲突:初始的
player_move是列表,后来被赋值为输入字符串,建议重命名初始列表为valid_moves避免混淆 game函数未定义:需要补充物品收集的具体逻辑,比如定义玩家背包列表,收集时添加进去
完整修复后的示例代码
rooms = { 'Laundry Room': {'West': 'Basement'}, #Game Starts Here 'Basement': {'North': 'Kitchen'}, 'Kitchen': {'West': 'Living Room', 'East': 'Dining Room', 'North': 'Bedroom', 'South': 'Basement'}, 'Living Room': {'East': 'Kitchen', 'item': '$100 cash'}, 'Dining Room': {'West': 'Kitchen', 'North': 'Backyard', 'item': '$6 cash'}, 'Bedroom': {'South': 'Kitchen', 'East': 'Bathroom', 'item': '$200 cash'}, 'Bathroom': {'West': 'Bedroom', 'item': '$300 cash'}, 'Backyard': {'South': 'Dining Room', 'item': '$60 cash'} #Return to the Laundry Room via the Basement } valid_moves = ['North', 'South', 'East', 'West'] currentRoom = 'Laundry Room' player_inventory = [] # 定义收集物品的函数 def collect_item(item): player_inventory.append(item) print(f"你获得了{item}!当前背包:{player_inventory}") # While Loop for moving between rooms while True: # 显示当前房间信息 print(f"\n当前房间:{currentRoom}") if 'item' in rooms[currentRoom]: print(f"房间里有:{rooms[currentRoom]['item']}") player_move = input('输入移动方向(North/South/East/West)或Exit退出:\n') if player_move == 'Exit': print('Thanks for playing.') break # 检查输入的方向是否有效 if player_move in valid_moves: # 检查当前房间是否有该出口 if player_move in rooms[currentRoom]: currentRoom = rooms[currentRoom][player_move] # 检查新房间是否有物品 if 'item' in rooms[currentRoom]: choice = input(f"发现物品{rooms[currentRoom]['item']},是否收集?(Y/N)\n").strip().upper() if choice == 'Y': collect_item(rooms[currentRoom]['item']) # 移除物品,防止重复收集 del rooms[currentRoom]['item'] else: print('那个方向没有门!') else: print('无效输入,请输入正确的方向或Exit!')
内容的提问来源于stack exchange,提问作者user22582701
相关产品推荐
相关产品推荐

