课堂文本冒险游戏Inventory系统报错求助:unhashable错误解决
文本冒险游戏Inventory系统问题修复方案
问题概述
开发基于文本的冒险游戏时,添加Inventory系统后出现unhashable错误。尝试用tuple解决但无法动态添加物品,需修复该问题并保留列表作为Inventory的可变特性。
错误原因分析
- 返回值赋值错误:
player_commands函数返回(current_room, inventory)元组,但调用时仅赋值给current_room,导致current_room变为元组类型。后续访问castle_rooms[current_room]时,元组虽可哈希但不存在于字典中,引发错误且破坏房间状态正确性。 - 物品判断逻辑错误:
get命令中if direction in current_room是检查物品名是否包含在房间名字符串中,而非判断当前房间是否存在该物品,逻辑完全错误。
修复步骤
- 修正返回值赋值:调用
player_commands时同时接收current_room和inventory两个返回值,保持current_room为字符串类型。 - 重构物品获取逻辑:判断当前房间的
Item字段是否等于目标物品,且该物品未被收集,满足条件才添加到Inventory并给出对应提示。 - 补充错误提示:处理物品已收集、房间无该物品的情况,给玩家明确反馈。
修正后的完整代码
def player_instructions(): # main menu and game commands print('Move Commands: go North, go South, go West, go East') print("Add to inventory: get 'item name'.") print('-' * 50) print('A Paladin Adventure Game.') # game opening greeting print('Collect 7 items to win the game,') print('or be destroyed by the Vampire Lord.') print('-' * 50) # dictionary for rooms and movement between castle_rooms = {'Entry Hall': {'East': 'Great Hall', 'South': 'Solar', 'Item': ''}, 'Great Hall': {'West': 'Entry Hall', 'South': 'Boudoir', 'North': 'Kitchen', 'East': 'Cabinet', 'Item': 'Silver Mail'}, 'Solar': {'North': 'Entry Hall', 'East': 'Boudoir', 'Item': 'Stake'}, 'Boudoir': {'North': 'Great Hall', 'West': 'Solar', 'Item': 'Holy Water'}, 'Kitchen': {'East': 'Larder', 'South': 'Great Hall', 'Item': 'Mallet'}, 'Larder': {'West': 'Kitchen', 'Item': 'Potion of Cleansing'}, 'Cabinet': {'West': 'Great Hall', 'North': 'Garderobe', 'South': 'Bedchamber', 'Item': 'Blessed Sword'}, 'Garderobe': {'South': 'Cabinet', 'Item': 'Torch'} } current_room = 'Entry Hall' # define starting room inventory = [] # define list for inventory def player_commands(command, direction): # function for moving rooms and managing inventory global current_room global inventory if command == 'go': if direction in castle_rooms[current_room]: current_room = castle_rooms[current_room][direction] else: print("You can't go that way") elif command == 'get': room_item = castle_rooms[current_room]['Item'] if room_item == direction and room_item not in inventory: inventory.append(direction) print(f'You have collected {direction}!') elif room_item in inventory: print('You already have this item.') else: print('That item is not in this room.') else: print('Invalid command') return current_room, inventory while current_room != 'Bedchamber': # while loop for gameplay player_instructions() print(f'You are in the {current_room}.') print(f'Inventory: {inventory}') # Show current inventory room_item = castle_rooms[current_room]['Item'] if room_item and room_item not in inventory: print(f'You see {room_item} in the room.') user_input = input('What will you do next? ').split() command = user_input[0] if len(user_input) > 0 else '' direction = user_input[1] if len(user_input) > 1 else '' current_room, inventory = player_commands(command, direction)
额外优化说明
- 添加Inventory实时显示,让玩家清楚当前收集的物品。
- 处理空物品房间(如Entry Hall)的提示逻辑,避免显示空字符串。
- 优化输入处理,防止用户输入空内容导致索引错误。
内容的提问来源于stack exchange,提问作者Daelinia
相关产品推荐
相关产品推荐

