James Vella
James Vella

Reputation: 47

Add Dictionary to Class

I would like to have my character add items to their inventory from a dictionary, however when I add them it only gives me the 'value' return, not the 'key' as well. I would like to use both for different parts of the game "You are holding 'key', it does 'value' damage" for example.

I think I understand this is adding the dictionaries 'value' because my inventory type is a 'list', but having trouble finding a simple way to add the dictionary items.

Thanks in advance for the help!

from random import randint

class Character:
    # Instantiates each character with personality traits
    def __init__(self, name, health, lives, gender, inventory):
        self.name = name
        self.health = int(health)
        self.lives = int(lives)
        self.gender = gender
        self.inventory = []


inventoryitems = {
    'Breadstick': randint(0,5), 
    'Magic Axe': randint(50,100), 
    'Tiny Fists': randint(0,25),
    'Sword': randint(15,30)
    }

player = Character("Kate", 100, 3, 'female',None)

player.inventory.append(inventoryitems['Magic Axe'])
player.inventory.append(inventoryitems['Breadstick'])
print(player.inventory)

Upvotes: 0

Views: 88

Answers (2)

NOhs
NOhs

Reputation: 2830

You can use the function items() to iterate over both the keys and the values at the same time:

from random import randint

class Character:
    # Instantiates each character with personality traits
    def __init__(self, name, health, lives, gender, inventory):
        self.name = name
        self.health = int(health)
        self.lives = int(lives)
        self.gender = gender
        self.inventory = []


inventoryitems = {
    'Breadstick': randint(0,5), 
    'Magic Axe': randint(50,100), 
    'Tiny Fists': randint(0,25),
    'Sword': randint(15,30)
}

player = Character("Kate", 100, 3, 'female',None)

# item will be a (key, value) pair
for item in inventoryitems.items():
    player.inventory.append(item)

# or if you only want to add specific items
items_to_add = ['Breadstick', 'Magic Axe']
for item in items_to_add:
    player.inventory.append((item, inventoryitems[item]))

print(player.inventory)

will print e.g.

[('Breadstick', 5), ('Magic Axe', 51), ('Tiny Fists', 21), ('Sword', 17), ('Breadstick', 5), ('Magic Axe', 51)]

Note how adding items twice will not apply new random values to each one, which might not be what you are looking for.

Upvotes: 1

bboumend
bboumend

Reputation: 510

Using dictionary into your class is simple and then you can generate list of item by name using player.inventory.keys(), you also can access item in a very easy and fast way later by doing player.inventory['object_name']

from random import randint

class Character:
    # Instantiates each character with personality traits
    def __init__(self, name, health, lives, gender, inventory):
        self.name = name
        self.health = int(health)
        self.lives = int(lives)
        self.gender = gender
        self.inventory = {}


inventoryitems = {
    'Breadstick': randint(0,5), 
    'Magic Axe': randint(50,100), 
    'Tiny Fists': randint(0,25),
    'Sword': randint(15,30)
    }

player = Character("Kate", 100, 3, 'female',None)

player.inventory['Magic Axe'] = inventoryitems['Magic Axe']
player.inventory['Breadstick'] = inventoryitems['Breadstick']
print(player.inventory)

Upvotes: 1

Related Questions