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

Python文字冒险游戏背包道具打印为单个字符而非完整单词如何修复

问题原因

核心问题出在道具收集的代码逻辑:
你使用了inventory += str(rooms[current_room]['item'])的写法给背包添加道具,在Python中,列表使用+=拼接可迭代对象时,会将字符串(字符串本身是字符组成的可迭代序列)拆分为单个字符,逐个添加到列表中,因此才会出现背包里存的是单个字符而非完整道具名的问题。

需修复的其他隐藏问题
  1. 方向判断逻辑错误:当前代码的elif move == 'North' or 'South' or 'East' or 'West'写法不符合Python语法规则,非空字符串会被直接判定为True,无论输入什么内容都会进入该分支。
  2. 无用冗余代码:你定义的main()函数没有实际调用,函数内部声明的inventory是局部变量,不会对全局的背包变量产生任何效果,属于无效代码。
修复方案
  1. 替换道具添加的写法:将inventory += str(rooms[current_room]['item'])改为inventory.append(str(rooms[current_room]['item'])),append()方法会将整个字符串作为单个元素添加到列表中,不会拆分字符。
  2. 修改方向判断逻辑:将elif move == 'North' or 'South' or 'East' or 'West'改为elif move in ('North', 'South', 'East', 'West')。
  3. 删除定义main()函数的无效代码段,同时移除方向判断分支里多余的重复输入逻辑。
修复后完整代码
def instructions():
    print('You are taking part in a museum heist!')
    print("Collect all 6 paintings before you find your mafia boss, or you will get fired!")
    print("To move, type: North, South, East, or West.")
    print("To collect an item, type: Collect")
    print('To quit, type Quit')

instructions()
print('----------------------')


def status():
    print('You are in the', current_room, 'Room')
    print('Inventory:', inventory)
    if 'item' in rooms[current_room]:
        print('You see an artwork called', rooms[current_room]['item'])


print('----------------------')

inventory = []#placeholders
item = ' '

rooms = {
    'Foyer': {'East': 'Contemporary'},
    'Contemporary': {'North': 'Impressionist', 'East': 'Abstract', 'West': 'Foyer', 'South': 'Surrealist', 'item': 'Self Portrait'},
    'Abstract': {'North': 'Pop Art', 'West': 'Contemporary', 'item': 'Convergence'},
    'Pop Art': {'South': 'Abstract', 'item': 'Shot Marilyns'},
    'Surrealist': {'North': 'Contemporary', 'East': 'Realist', 'item':  'The Persistence of Memory'},
    'Impressionist': {'South': 'Contemporary', 'East': 'Expressionist', 'item': 'Poppies'},
    'Expressionist': {'West': 'Impressionist', 'item': 'Starry Night'},
    'Realist': {'West': 'Surrealist', 'item': 'Mafia Boss'}
}

current_room = 'Foyer'

while True:
    status()
    if rooms[current_room] == rooms['Realist']:
        print('Oh no! Your boss is angry you did not steal all the paintings! You have been fired. GAME OVER.')
        break
    if len(inventory) == 6:
        print('Congratulations! You got all the paintings. You Win!!')
        break
    print('----------------------')
    print('Which way?')
    move = input()
    if move == 'Quit':
        print('Sorry to see you go!')
        break
    elif move == 'Collect':
        if 'item' in rooms[current_room]:
            inventory.append(rooms[current_room]['item'])
            print("You collected", rooms[current_room]['item'])
            del rooms[current_room]['item']
        else:
            print('There is no item here.')
    elif move in ('North', 'South', 'East', 'West'):
        if move in rooms[current_room]:
            current_room = rooms[current_room][move]
        else:
            print("Sorry, you can't go that way.")
    else:
        print('Invalid move!')

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 10:45:02