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:

JavaScript

My issue is here, in my get_items function:

JavaScript

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:

JavaScript

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:

JavaScript

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