【问题标题】:Adding items to an inventory [closed]将项目添加到库存[关闭]
【发布时间】:2021-04-19 17:35:21
【问题描述】:

我正在尝试为班级构建一个简单的基于文本的游戏,并且需要将项目添加到库存中。它应该阅读房间字典以找到该项目,然后如果键入 get Item 则将该项目添加到玩家库存中。在第一个房间时应该这样输出:

You are in Entrance Hall
Inventory [Items]
You Don't see anything useful
What do you wish to do?

我似乎无法弄清楚如何让我的代码这样做。我可能引用了一些错误的东西,但我对编码很陌生,所以我看不出我哪里出了问题。我的代码如下,非常感谢任何帮助。

# A dictionary for the simplified text game that links a room to other rooms.
rooms = {
        'Entrance Hall': {'North': 'Great Hall', 'East': 'Gallery', 'West': 'Library'},
        'Library': {'East': 'Entrance Hall', 'item': 'Book'},
        'Gallery': {'West': 'Entrance Hall', 'item': 'Sword'},
        'Great Hall': {'South': 'Entrance Hall', 'East': 'Dining Room', 'West': 'Cellar', 'item': 'Armor'},
        'Dining Room': {'North': 'Kitchen', 'West': 'Great Hall', 'item': 'Plates'},
        'Kitchen': {'South': 'Dining Room', 'item': 'Knife'},
        'Cellar': {'North': 'Catacombs', 'East': 'Great Hall', 'item': 'Ale'},
        'Catacombs': {'South': 'Cellar'}
    }

instructions = 'A Necromancer has moved into your catacombs to raise an army of the undead! ' \
               'You need to defeat him before he turns the dead against you and the town! ' \
               'Before you can fight him,you are going to need 5 items to aid you. ' \
               'You will need armor to protect your body against his attacks, ' \
               'a book to learn how to protect yourself from his spells,' \
               ' a sword to defeat him, a candle to see in the dark,' \
               'and a bottle of Ale to gather your courage and take on this task. ' \
               'To move type: go North, go East, go West, go South' \
               'to get items type get Item, ex: get Sword\n '

directions = ['go North', 'go South', 'go East', 'go West']
pick_up_items = ['get Ale', 'get Book', 'get Armor', 'get Sword', 'Necromancer', 'get Knife', 'get Candle']

print(instructions)
current_room = 'Entrance Hall'
item_in_room = 'None'
inventory = []

while True:
    if inventory == ['Ale', 'Book', 'Armor', 'Sword', 'Candle']:
        print('Congratulations! You collected all the items, you use you wits to head into the'
              'cellar and defeat the Necromancer!')
    elif current_room == 'Catacombs':
        print('A bolt of magic streaks towards you and your vision goes black..... Game Over!')
        break
    # displays the players current location
    print('You are in the {}.'.format(current_room))
    print('Inventory:', inventory)
    if item_in_room:  #checks to see if there is an item in the room
        print('You see a'.format(item_in_room))
    else:
        print("You don't see anything useful")

    # gets the users input
    command = input('\nWhat do you wish to do? ')  # this controls the movement
    if command in directions:
        command = command.split()[1]
        if command in rooms[current_room].keys():
            current_room = rooms[current_room][command]
        else:
            # if the player inputs a bad movement
            print('You cant go that way!')
    # Checks to see if the player types a 'get' command and adds the item in the room to the players inventory.
    if command in pick_up_items:
        command = command.split()[1]
        if command in rooms[current_room].keys():
            inventory = current_room['item']
        else:
            # if the player inputs a item not in the room
            print('You cant get that item here!')

【问题讨论】:

    标签: python inventory


    【解决方案1】:

    我希望我能正确理解您的问题,您希望将项目添加到名为 inventory 的列表中。

    在您的代码中:

    if command in rooms[current_room].keys():
            inventory = current_room['item']
    

    您将库存设置为等于该项目,但我认为您希望完成的是将一个项目添加到列表中。所以你应该这样做:

    inventory.append(current_room['item'])
    

    这会将项目添加/附加到列表中。

    我希望这就是您想要的。 并且请不要在以后发布您的完整代码,只发布所需的部分 并且请在您的查询中更加隐含。

    祝你有美好的一天。

    【讨论】:

    • 是的,我还是新来的堆栈溢出,所以我不知道要包含哪些代码。我遇到的唯一另一个问题是它似乎没有读取每个房间里的物品。我将如何解决这个问题?
    • 您的 item_in_room 变量在程序中的任何地方都不会更改,因此,if else 条件始终评估为 false。
    • 如果这个答案对你有帮助,请点击对答案的投票,因为它也会对我有很大帮助。 :)
    • 您需要房间中的项目列表而不是字典,如下所示:'Entrance Hall': {'North': 'Great Hall', 'East': 'Gallery', 'West': 'Library', "items": ["item1", "item2", ....] }
    • 然后你会这样做:if command in rooms[current_room].keys(): current_room = rooms[current_room][command] item_in_room = rooms[current_room]["items"]
    猜你喜欢
    • 2012-01-30
    • 2019-08-12
    • 2022-01-17
    • 2013-06-20
    • 2015-01-26
    • 2020-09-05
    • 1970-01-01
    • 2012-09-23
    • 2014-01-07
    相关资源
    最近更新 更多