You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python文本游戏中item_list.remove()失效但append()正常的问题求助

文本类小游戏物品拾取模块问题排查

问题描述

正在完成Python入门课堂项目:一款文本类小游戏,房间移动逻辑正常,但物品/背包模块存在问题。设计逻辑为玩家输入Get item指令后,若当前房间物品在item_list中,则将其添加到inventory并从item_list移除,避免重复拾取。目前inventory.append()能正常添加物品,但视觉上感觉item_list.remove()毫无作用,回到原房间仍显示有物品。

问题代码

rooms = { #defines the locations, the relationships between the locations, and locates the items not yet in inventory
        'Great Hall':
                {'South': 'South Hall', 'North': 'North Hall', 'West': 'Entrance', 'East': 'Kitchen'},
        'South Hall':
                {'North': 'Great Hall', 'East': 'Dungeon', 'item': 'a Mirror'},
        'Dungeon':
                {'West': 'South Hall', 'item': 'a Shield'},
        'North Hall':
                {'South': 'Great Hall', 'East': 'Tower', 'item': 'a Hand Mirror'},
        'Tower':
                {'West': 'North Hall', 'item': "Odin's Staff"},
        'Kitchen':
                {'West': 'Great Hall', 'North': 'Dining Room', 'item': 'a bowl of Nuts'},
        'Entrance':
                {'East': 'Great Hall', 'item': 'a Torch'},
        'Dining Room':
                {'South': 'Kitchen'}}
directions = ['North', 'South', 'East', 'West'] #defines player movement options
item_list = ['a Mirror', 'a Shield', 'a Hand Mirror', "Odin's Staff", 'a bowl of Nuts', 'a Torch'] #reference for the 'Get item' command
inventory = [] #list to be populated with items as they are collected by the player
current_room = 'Entrance' #starting point, updates to output current location when player moves
def player_status():
        print('You are in the', current_room)  # status
        print('Inventory:', inventory)
        print('You see:', rooms.get(current_room).get('item'))
while True:
        if current_room == 'Dining Room': #win condition
                if len(inventory) == 6:
                        print('Win condition text')
                else: #lose condition
                        print('Lose condition text')
                break

        player_status()
        command = input('What would you like to do?')
        if command in directions: #moves player in the direction indicated by input.
                if command in rooms.get(current_room).keys():
                        current_room = rooms[current_room].get(command)
                else: #shows if input is not in dictionary
                        print('There is nothing in that direction.')
        elif command == 'Get item': #The command for adding an item to inventory.
                if rooms.get(current_room).get('item') in item_list:
                        inventory.append(rooms.get(current_room).get('item'))
                        item_list.remove(rooms.get(current_room).get('item'))
                        print('You acquired:',rooms.get(current_room).get('item'))
                else:
                    print("There's nothing here.")
        else:
                print("I'm sorry, I don't understand that command")

问题原因

  1. item_list.remove()实际是生效的:你可以在执行该语句后打印item_list验证,元素确实会被移除。
  2. 视觉误解的根源:你只修改了item_list,但没有更新rooms字典中当前房间的item字段。玩家回到房间时,player_status()函数仍然会从rooms字典中读取到原物品,导致你误以为物品没被移除。
  3. 额外体验问题:当房间没有物品时,player_status()会显示You see: None,交互体验不够友好。

解决方案

方案一:修复现有逻辑,同步更新房间物品状态

在拾取物品时,除了修改item_list和inventory,还要删除当前房间的item键:

elif command == 'Get item':
    room_item = rooms.get(current_room).get('item')
    if room_item in item_list:
        inventory.append(room_item)
        item_list.remove(room_item)
        # 新增:删除房间中的物品,同步状态
        del rooms[current_room]['item']
        print('You acquired:', room_item)
    else:
        print("There's nothing here.")

同时优化player_status()函数,处理无物品的情况:

def player_status():
    print('You are in the', current_room)
    print('Inventory:', inventory)
    room_item = rooms.get(current_room).get('item')
    if room_item:
        print('You see:', room_item)
    else:
        print('You see nothing of interest here.')

方案二:移除冗余的item_list,简化逻辑

item_list属于冗余数据源,直接通过inventory和房间的item字段判断即可,无需额外维护参考列表:

# 可以删除这一行冗余代码:item_list = ['a Mirror', 'a Shield', 'a Hand Mirror', "Odin's Staff", 'a bowl of Nuts', 'a Torch']

# 修改Get item的逻辑
elif command == 'Get item':
    room_item = rooms.get(current_room).get('item')
    if room_item and room_item not in inventory:
        inventory.append(room_item)
        del rooms[current_room]['item']
        print('You acquired:', room_item)
    else:
        print("There's nothing here.")

这种方式更简洁,避免了维护两个数据源可能带来的不一致问题。

内容的提问来源于stack exchange,提问作者Phillip Steinberg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 20:21:08