微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将项目添加到库存

如何解决将项目添加到库存

我正在尝试为课堂构建一个简单的基于文本的游戏,并且需要将项目添加到库存中。它应该阅读房间字典以找到该项目,然后如果输入 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!')

解决方法

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

在您的代码中:

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

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

inventory.append(current_room['item'])

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

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

祝您有美好的一天。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。