简体   繁体   English

构建 python 基于文本的游戏:我试图访问字典中的特定值

[英]building a python text-based game: im trying to access a specific value inside of a dictionary

hey everybody i am building a text based game for a class where i am moving between rooms and trying collect all the items without going into the room with the villian, in this case the Record Executive, and im just having issues accessing the item directly so that I can add it my inventory list..嘿大家我正在为 class 构建一个基于文本的游戏,我在房间之间移动并尝试收集所有物品而无需与恶棍一起进入房间,在这种情况下是 Record Executive,我只是在直接访问该项目时遇到问题所以我可以将它添加到我的库存清单..

here is my dictionary:这是我的字典:

rooms = {
    'Lobby':{'East': 'Bar'},
    'Roof':{'South': 'Bar', 'East': 'Stage', 'Item': 'Steel guitar'},
    'Bar':{'West': 'Lobby', 'North': 'Roof', 'East': 'Bathroom', 'South': 'Basement', 'Item': 'Keyboard'},
    'Basement':{'North': 'Bar', 'East': 'Studio', 'Item': 'Drums'},
    'Stage':{'West': 'Roof', 'Item': 'Microphone'},
    'Soundcheck':{'South': 'Bathroom', 'Item': 'Bass Guitar'},
    'Bathroom':{'North': 'Soundcheck', 'West': 'Bar', 'Item': 'Electric Guitar'},
    'Studio': {'West': 'Basement', 'Item': 'Record Executive'}
}

heres my movement code:这是我的运动代码:

while gameOver != True:
    playerStats(currentRoom, inventory, rooms)
    # currentRoom
    playerMove = input('Enter your move:\n')

    try:
        currentRoom = rooms[currentRoom][playerMove]
    except Exception:
        print('Invalid move')
        continue

I move East from Lobby and my current room changes to Bar like it should but I don't know how to get to the 'Item' property in it so I can add 'Keyboard' to my inventory list... any help would be much appreciated!!我从大厅向东移动,我现在的房间按原样更改为酒吧,但我不知道如何进入其中的“项目”属性,所以我可以将“键盘”添加到我的库存列表中......任何帮助都是非常感激!! thank you谢谢你

To index the dictionary you want索引你想要的字典

currentRoom['Item']

You will add it to the list after doing the move, so at the end (after the "try", "except", part) you will add:您将在移动后将其添加到列表中,因此最后(在“尝试”、“除外”部分之后)您将添加:

    inventory.append(currentRoom['Item'])

By putting it after the movement code, you take the item from the new room, not the existing room.通过将其放在移动代码之后,您可以从新房间而不是现有房间中取出物品。

Some rooms might not have an item, so then you can do:有些房间可能没有物品,所以你可以这样做:

    try:
        inventory.append(currentRoom['Item'])
    except KeyError:
        pass

This prevents an error when you move into the room with no item.这可以防止当您搬进没有物品的房间时出错。

Also you might want to remove the item from the room when you take it.此外,您可能希望在拿取物品时将其从房间中移除。 So you can do.所以你可以做到。

    try:
        inventory.append(currentRoom['Item'])
        del currentRoom['Item']
    except KeyError:
        pass

The del removes the key from the dictionary del从字典中删除键


By the way, in your movement code, you should also change顺便说一句,在你的运动代码中,你也应该改变

    except Exception:

to

    except KeyError:

So if there is a different exception (eg you press ctrl-C) then it does not catch it.因此,如果有不同的异常(例如,您按 ctrl-C),则它不会捕获它。

just like this command就像这个命令

currentRoom = rooms[currentRoom][playerMove]

you can try to append the item in the room to the variable (inventory), preferrable a list (or a set if that fulfills your requirement)您可以尝试 append 房间中的项目到变量(库存),最好是一个列表(或一组如果满足您的要求)

inventory = []

for i in rooms:
    for j in i:
        if j == 'Item':
            inventory.append(rooms[i][j])

now it shall look like this:现在它看起来像这样:

inventory = []
gameOver = False

rooms = {
    'Lobby':{'East': 'Bar'},
    'Roof':{'South': 'Bar', 'East': 'Stage', 'Item': 'Steel guitar'},
    'Bar':{'West': 'Lobby', 'North': 'Roof', 'East': 'Bathroom', 'South': 'Basement', 'Item': 'Keyboard'},
    'Basement':{'North': 'Bar', 'East': 'Studio', 'Item': 'Drums'},
    'Stage':{'West': 'Roof', 'Item': 'Microphone'},
    'Soundcheck':{'South': 'Bathroom', 'Item': 'Bass Guitar'},
    'Bathroom':{'North': 'Soundcheck', 'West': 'Bar', 'Item': 'Electric Guitar'},
    'Studio': {'West': 'Basement', 'Item': 'Record Executive'}
}


while not gameOver:
    playerStats(currentRoom, inventory, rooms)
    # currentRoom
    playerMove = input('\n Enter your move: ')

    if playerMove == 'pick' or playerMove == 'acquire' or playerMove == 'get':
        # add to inventory if exists
        for i in rooms:
            for j in i:
                if j == 'Item':
                    inventory.append(rooms[i][j])
                else:
                    print('nothing to pick')
    else:
        try:
            currentRoom = rooms[currentRoom][playerMove]
        except Exception:
            print('Invalid move')
            continue

EDIT:编辑:

Also you shall now delete the item from the room, since it is now acquired--你现在也应该从房间里删除这个项目,因为它现在已经被收购了——

(As also pointed out by @Addlestrop in their answer) (正如@Addlestrop 在他们的回答中指出的那样)

        for i in rooms:
            for j in i:
                if j == 'Item':
                    inventory.append(rooms[i][j])
                    del rooms[i][j]
                    # copy item to inventory and delete from the room
                else:
                    print('nothing to pick')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM