Skip to content
Advertisement

Retrieve specific fields of a namedtuple instance from a nested list in Python

I am starting a project for school, a basic text-adventure game, where the player navigates through several rooms, each with a set of items, and adds an item to their inventory by spending a turn.

Since each item is unique to a room and each item is also unique in its stat benefits, I have done the following to implement these:

Item = namedtuple('Item', ['name', 'atk', 'defense', 'agi', 'hp'])

#Sport Store Items
bat = Item('Baseball Bat', 3, 1, 0, 0)
shoes = Item('Running Shoes', 0, 1, 3, 0)
dryeggs = Item('Freeze Dried Eggs', 0, 0, 1, 2)


#Clothes Store Items
belt = Item('Studded Belt', 1, 1, 0, 0)
candy = Item('Japanese Candy', 0, 0, 1, 1)
jacket = Item('Leather Jacket', 0, 3, 0, 1)

#Toy Store Items:
cars = Item('Toy Car Pack', 1, 1, 0, 0)
crayons = Item('Crayons', 0, 0, 1, 0)
toygun = Item('Toy Gun', 2, 1, 0, 0)

#Candle Store Items:
jar = Item('Candle Jar', 2, 0, 0, 0)
matches = Item('Matches', 1, 0, 1, 0)
wax = Item('Wax', 0, 2, 1, 0)

#Music Store Items:
disc = Item('Vinyl Disc', 2, 0, 1, 0)
guitar = Item('Electric Guitar', 3, 0, 0, 0)
symbol = Item('Symbol', 1, 2, 0, 0)

all_items = [
    [bat, shoes, dryeggs, '1'],
    [belt, candy, jacket, '2'],
    [cars, crayons, toygun, '3'],
    [jar, matches, wax, '4'],
    [disc, guitar, symbol, '5']
]

My issue is here, in my get_items function:

def get_items(id):
    for i in all_items:
        if i[3] == id:
            items = i
            items = list(items[0:4])
            return items
        else:
            continue

I’m trying to get the list of items based on the matching ID parameter. There is a function in another file that takes the player’s current position and sends it to other functions as the map_id. I’ve successfully made it so the name of the store changes based on this position, but I cannot figure out how to handle these sub-lists to return a specific sub-list based on the id.

For example, in the sport_store code, I’m trying to return the results like so:

def sport_store():
    room_id = '1'

    item_select = items.get_items(room_id)
    #FIXME: import items.py later with this function in it.
    if item_select != []:
    
        if entering == True:
            print('You enter the Sporting Goods Store.')
            print('There aren't many supplies left. What will you pick?')
        else:
            print('There aren't many supplies left. What will you pick?')

        print(item_select)

However, no matter what things I change in get_items’ loop to get it to work, I always return the original empty list of item_selection. I am trying to select the matching sub-list from the all_items global with the get_items function and then use that sub-list to make a new list that gets sent to item_selection showing the items for that store, and then format the namedtuples in it to show just the names value. Is this possible or do I need to convert the data to a dictionary?

Advertisement

Answer

you can use a dictionary for all items:

all_items = {
    '1':[bat, shoes, dryeggs],
    '2':[belt, candy, jacket],
    '3':[cars, crayons, toygun],
    '4':[jar, matches, wax],
    '5':[disc, guitar, symbol]
}

then, instead of calling items.getItems(room_id), you could just do all_items[room_id]. Hope this helps!

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement