应为类型“str”,而得到的是“List[str]”

2024-09-30 01:27:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在PyCharm中创建一个基于文本的冒险游戏,我现在编写的代码是当玩家打开一组抽屉查看里面。我想让他们知道他们打开的抽屉里有多少个项目,以及这些项目是什么;但是我一直收到一条消息,说Expected type 'str', got 'List[str]' instead。在

我怎么解决这个问题?问题就在代码的底部。在

if "go to drawers" in choice:
    drawerStates = '1 and 2 are open. 3 is missing a handle.'
    drawer1Inventory = ['lipstick', 'cute photo of a dog', 'socks']
    drawer2Inventory = ['T-shirt', 'jeans', 'pants', 'socks']
    drawer3Inventory = ['key']
    subLocation = 'drawers'
    slowprint('You go to the set of drawers.')
    time.sleep(1)
    slowprint('''They're very old and worn down. Only 2 out of the 3 have handles.
     You need to find the third handle to open it.''')

    while subLocation == 'drawers':
        slowprint('What would you like to do? (Type help for help)')
        choice = input('>')
        if 'location' in choice:
            slowprint('You are at the ' + subLocation + ' in the ' + location)

        if 'help' in choice:
            slowprint('Right now drawers ' + drawerStates)
            slowprint('''Type 'open drawer 1' to open the 1st drawer.
            Type 'open drawer 2' to open the 2nd.
            Type 'open drawer 3' to open the 3rd''')

        if 'open drawer 1' in choice:
            slowprint('Currently, there are ' + len(drawer1Inventory) + 'items in this drawer.')
            slowprint('Items in drawer 1:' + drawer1Inventory)

Tags: ofthetoinyouiftypeopen
2条回答

抽屉1库存是一个列表。如果要打印项,请尝试使用join,这将获取所有列表项,并输出一个字符串,其中每个列表项用逗号分隔:

^{1}$

在python中,不能添加字符串和整数。在

你应该更换

^{1}$

签署人:

^{pr2}$

此外,不能添加字符串和列表,应替换:

slowprint('Items in drawer 1:' + drawer1Inventory)

签署人:

slowprint(f'Items in drawer 1: {drawer1Inventory}')

(如果您使用的是python3.6+)

相关问题 更多 >

    热门问题