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

Python房间移动功能报错:多出口房间无法正常跳转求助

问题:多出口房间移动触发TypeError错误

我尝试用字典实现房间间的移动,单个出口的房间能正常导航,但存在多个出口的房间会触发错误。

原实现代码

# The dictionary links a room to other rooms.
rooms = {
    'Great Hall': {'South': 'Bedroom'},
    'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
    'Cellar': {'West': 'Bedroom'}
}


def instructions():
    print("Milestone for Module 6.")
    print("Type 'go' and then the direction want to move in lowercase")
    print("You can type exit to leave the game after this menu")

current_room = 'Great Hall'
direction = ''

while True:
    print('\nYou are in:', current_room)

    direction = input('Move which direction?')
    print('You entered:', direction)

    if direction == 'quit':
        print('Game Exited')
        break

    elif direction == 'go north':
        if 'North' in rooms[current_room].keys():
            current_room = str(*rooms[current_room].values())
        else:
            print('Unable to move to that location, try again.')

    elif direction == 'go south':
        if 'South' in rooms[current_room].keys():
            current_room = str(*rooms[current_room].values())
        else:
            print('Unable to move to that location, try again.')

    elif direction == 'go east':
        if 'East' in rooms[current_room].keys():
            current_room = str(*rooms[current_room].values())
        else:
            print('Unable to move to that location, try again.')

    elif direction == 'go west':
        if 'West' in rooms[current_room].keys():
            current_room = str(*rooms[current_room].values())
        else:
            print('Unable to move to that location, try again.')
    else:
        print('Invalid, please try again.')

报错信息

Traceback (most recent call last):
  line 40, in <module>
    current_room = str(*rooms[current_room].values())
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: decoding str is not supported

错误原因

问题出在current_room = str(*rooms[current_room].values())这行代码:

  • 当房间只有一个出口时,rooms[current_room].values()返回单个值的视图,解包后传给str()不会报错,但这是巧合;
  • 当房间有多个出口时,values()返回多个值,用*解包后str()会接收到多个参数,而str()仅支持单个位置参数,因此触发TypeError。

修复后的代码

# The dictionary links a room to other rooms.
rooms = {
    'Great Hall': {'South': 'Bedroom'},
    'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
    'Cellar': {'West': 'Bedroom'}
}


def instructions():
    print("Milestone for Module 6.")
    print("Type 'go' and then the direction want to move in lowercase")
    print("You can type exit to leave the game after this menu")

current_room = 'Great Hall'
direction = ''

while True:
    print('\nYou are in:', current_room)

    direction = input('Move which direction?')
    print('You entered:', direction)

    if direction == 'quit':
        print('Game Exited')
        break

    elif direction == 'go north':
        if 'North' in rooms[current_room]:
            current_room = rooms[current_room]['North']
        else:
            print('Unable to move to that location, try again.')

    elif direction == 'go south':
        if 'South' in rooms[current_room]:
            current_room = rooms[current_room]['South']
        else:
            print('Unable to move to that location, try again.')

    elif direction == 'go east':
        if 'East' in rooms[current_room]:
            current_room = rooms[current_room]['East']
        else:
            print('Unable to move to that location, try again.')

    elif direction == 'go west':
        if 'West' in rooms[current_room]:
            current_room = rooms[current_room]['West']
        else:
            print('Unable to move to that location, try again.')
    else:
        print('Invalid, please try again.')

关键修改说明

  • 移除了错误的str(*rooms[current_room].values())写法,改为根据用户输入的方向,直接从当前房间的字典中取出对应的目标房间(比如rooms[current_room]['North']);
  • 简化了存在性判断:'North' in rooms[current_room]等价于'North' in rooms[current_room].keys(),代码更简洁。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 13:05:17