Question: I have been trying to come up with the code to add an item to my inventory in my text based game but so far I haven't been able to figure it out. Here is my current code: rooms = { 'Entrance': {'west': 'Catacombs A', 'north': 'Main Hall'}, 'Catacombs A': {'east': 'Entrance', 'item': 'Lesser Artifact'}, 'Main Hall': {'north': 'Great Hall', 'east': 'Catacombs B', 'south': 'Entrance', 'west': 'Necron Tomb', 'item': 'Dog Tags'}, 'Necron Tomb': {'east': 'Main Hall', 'item': 'Supplies'}, 'Catacombs B': {'west': 'Main Hall', 'north': 'Storage Room', 'item': 'Lesser Artifact'}, 'Storage Room': {'south': 'Catacombs B', 'item': 'Supplies'}, 'Great Hall': {'east': 'Repair Hub', 'south': 'Main Hall', 'item': 'Dog Tags'}, 'Repair Hub': {'west': 'Great Hall', 'item': 'Necron Lord'} # Villain } current_room = 'Entrance' inventory = [] print('You are a member of a squad of Ultramarines that has been tasked with retrieving 6 items from a ' 'Necron facility on the orbiting planet before reaching the Necron Lord. You and your brothers breach the entrance, which way do you go first?') def move_rooms(current_room, directions): current_room = rooms[current_room] new_room = current_room[directions] return new_room while True: print('You are in the', current_room) directions = input('Enter a direction: north, east, south, west, get item, or exit.') if directions in rooms[current_room]: current_room = move_rooms(current_room, directions) if directions == 'exit': print('You have failed the Emperor and your brothers...') break elif directions not in rooms[current_room]: print('You cannot go that way.')  I tried making a define function for getting an item by doing def get_item(item, inventory): if item in current_room: inventory.append(item[current_room]) return inventory  Python gave me an error and asked to define item even though it is in my rooms dictionary. I am very new to python so I am struggling a lot, any help would be appreciated!

Answer: So I've been experimenting with this and i think i may have found a method that will work. I made a function that takes the currentroom, and your inventory. It will search the dictionary of that room to see if it has an item and if it does it will append it to the inventory table. The code is as follows: rooms = { 'Entrance': {'west': 'Catacombs A', 'north': 'Main Hall'}, 'Catacombs A': {'east': 'Entrance', 'item': 'Lesser Artifact'}, 'Main Hall': {'north': 'Great Hall', 'east': 'Catacombs B', 'south': 'Entrance', 'west': 'Necron Tomb', 'item': 'Dog Tags'}, 'Necron Tomb': {'east': 'Main Hall', 'item': 'Supplies'}, 'Catacombs B': {'west': 'Main Hall', 'north': 'Storage Room', 'item': 'Lesser Artifact'}, 'Storage Room': {'south': 'Catacombs B', 'item': 'Supplies'}, 'Great Hall': {'east': 'Repair Hub', 'south': 'Main Hall', 'item': 'Dog Tags'}, 'Repair Hub': {'west': 'Great Hall', 'item': 'Necron Lord'} # Villain } current_room = 'Storage Room' inventory = [] items = 'item' def get_item(currentroom, inventory): roomtosearch = rooms[currentroom] if items in roomtosearch: found = roomtosearch[items] inventory.append(found) else: print('No such item exists') get_item(current_room, inventory) print(inventory)  you can add an if statement in your while true loop that when given that direction input it if its "item" it will call the get_item() function searching that room for an item. Ex: while True: print('You are in the', current_room) directions = input('Enter a direction: north, east, south, west, get item, or exit.') if directions in rooms[current_room]: current_room = move_rooms(current_room, directions) if directions == 'exit': print('You have failed the Emperor and your brothers...') break if directions == 'item': get_item(current_room, inventory) elif directions not in rooms[current_room]: print('You cannot go that way.')  I saw your comment and i did some more testing and i realized that the sequence is messed up, because it searches for if item is in the room which it is, and it checks for that before it runs the get_item function it will set the current room to the item making the get_item throw an error. The solution to this is to rephrase the get_item statement to check before the moveroom one and if it the get_item check is true it wont run the moveroom function. My new code is as follows: while True: print('You are in the', current_room) directions = input('Enter a direction: north, east, south, west, get item, or exit.') if directions == 'item': print(current_room) get_item(current_room, inventory) elif directions in rooms[current_room]: current_room = move_rooms(current_room, directions) print(current_room) elif directions == 'exit': print('You have failed the Emperor and your brothers...') break elif directions not in rooms[current_room]: print('You cannot go that way.')