IT140脚本入门课程:文本游戏无法添加物品至inventory问题求助
问题分析与修复方案
核心问题点
- 'get'命令逻辑分支错误:当前代码仅在输入
get无后续物品名时才处理拾取逻辑,而用户实际输入get item name时params不为空,导致拾取代码完全未执行。 - 物品匹配判断错误:代码中用
'item' == room_item做匹配,将固定字符串'item'与房间物品名对比,永远无法匹配成功。 - 物品名拼接问题:输入的物品名可能包含空格(如
Fire Spell),直接取params[0]只能拿到第一个单词,无法匹配完整物品名。 - 状态显示错误:
player_status函数固定显示Entryway房间,无法正确展示当前所在房间的状态。
修复后的完整代码
rooms = { 'Entryway': {'East': 'Living Room'}, 'Living Room': {'West': 'Entryway', 'North': 'Bedroom', 'South': 'Kitchen', 'East': 'Theatre', 'item': 'Armor'}, 'Bedroom': {'East': 'Office', 'South': 'Living Room', 'item': 'Sword'}, 'Office': {'West': 'Bedroom', 'South': 'Basement', 'item': 'Fire Spell'}, 'Basement': {'North': 'Office', 'South': 'Theatre', 'item': 'Potion'}, 'Theatre': {'North': 'Basement', 'West': 'Living Room', 'item': 'Resistance Bracelet'}, 'Kitchen': {'North': 'Living Room', 'East': 'Dining Room', 'item': 'Ice Spell'}, 'Dining Room': {'West': 'Kitchen'} } inventory = [] villain_room = 'Dining Room' def moving_rooms(direction, player_room): global inventory global rooms if direction in rooms[player_room]: new_room = rooms[player_room][direction] print('You are in ', new_room) if 'item' in rooms[new_room]: item = rooms[new_room]['item'] print('You found the ', item) return new_room else: print('Wrong Way! Try another direction.') return player_room def show_instructions(): print('Welcome to The Goblin Game, GG!') print('Collect all 6 items to defeat the goblin or he eats all your food!') print('Move commands: go South, go North, go East, go West') print("Add to Inventory: get 'item name'") def player_status(): global player_room print(f'You are in the {player_room}') if 'item' in rooms[player_room]: print('There is an item: ', rooms[player_room]['item']) else: print('There is not an item in this room.') print(f'Inventory: {inventory}') def main(): global player_room show_instructions() print('-------------------') player_room = 'Entryway' player_status() while True: if len(inventory) == 6 and player_room == villain_room: print("GG! YOU WON!\n" "Congrats!\n" "You collected all items and defeated the goblin!\n" "---------------------------------------------------") break if player_room == villain_room and len(inventory) < 6: print("YOU LOSE!\n" "You couldn't defeat the goblin and he ate all your food....\n" 'Maybe if you had more items.') break move = input('What is your next move? ').lower() action, *params = move.split() print(f'Inventory: {inventory}') if action == 'go': direction = params[0].capitalize() if direction in rooms[player_room]: player_room = moving_rooms(direction, player_room) else: print('Wrong Way! Try another direction. (North, East, South, or West)') elif action == 'get': if len(params) == 0: print('I do not know that item, please try again.') continue # 拼接完整物品名并统一格式 requested_item = ' '.join(params).title() if 'item' in rooms[player_room]: room_item = rooms[player_room]['item'] if requested_item == room_item: inventory.append(room_item) del rooms[player_room]['item'] print(f'You picked up the {room_item}') print(f'Inventory: {inventory}') else: print('That item is not in here. Try searching somewhere else.') else: print('There is not an item in this room.') elif action == 'status': player_status() elif action == 'quit': break if __name__ == "__main__": main()
关键修复细节
- 重构'get'命令逻辑:将拾取逻辑从空参数分支中移出,优先处理带物品名的输入,空参数时仅提示错误并跳过后续逻辑。
- 正确拼接物品名:用
' '.join(params).title()将输入的多单词物品名拼接完整,并统一首字母大写,与房间内物品名格式对齐。 - 修正匹配条件:用用户输入的物品名与房间内实际物品名对比,替换原有的无效字符串对比。
- 修复状态显示:让
player_status函数使用全局的player_room变量,正确展示当前所在房间的状态。
内容的提问来源于stack exchange,提问作者user23479936
相关产品推荐
相关产品推荐

