Home > front end >  I am trying to figure out why I am getting the two error messages that I am, and how to fix them. I
I am trying to figure out why I am getting the two error messages that I am, and how to fix them. I

Time:04-15

I am having trouble with the errors in the picture. It says that my dictionary 'rooms' is not even being referenced. I am not sure what to do or how to fix this. I don't have a lot of experience with dictionaries. I am still learning python and I am struggling with this code. I am about to pull my hair out honestly.

# Define player location and state current inventory
def player_location():
    print('-' * 20)
    print('You are in the {}'.format(location))
    print('Inventory: ', inventory)
    print('-' * 20)

def get_new_location(location, player_move):
    new_state = location
    if location in rooms:
        if player_move in rooms[location]:
            new_state = rooms[location][player_move]

    return new_state

def get_room_item(location):
    return rooms[location]['Item']

# Function to show instructions and welcome players to the game.
def show_instructions():
    print("Welcome to the Saga of Light Text Adventure Game!")
    print("Collect 6 items to win the game, or be beaten by The Dark Elf Nebo.")
    print("Movement commands: North, South, East, West, or Exit to leave the Game.")
    print("Add to Inventory: Get 'Item'")


def main_gameplay():
    # Dictionary linking rooms and items obtained in them.
    rooms = {
    'Main Hall': {'South': 'Bedroom', 'North': 'library', 'East': 'Kitchen', 'West': 
    'Music Room'},
    'Music room': {'East': 'Main Hall', 'Item': 'Music Box'},
    'Bedroom': {'North': 'Main Hall', 'East': 'Safe room', 'Item': 'Cloak of Strength'},
    'Safe room': {'West': 'Bedroom', 'Item': 'Bow & Arrows'},
    'Dining Room': {'South': 'Kitchen', 'Item': 'The Dark Elf Nebo'},
    'Kitchen': {'West': 'Main Hall', 'North': 'Dining Room', 'Item': 'Energy Drink'},
    'Study': {'West': 'Library', 'Item': 'Ring of Light'},
    'Library': {'East': 'Study', 'South': 'Main Hall', 'Item': 'Book'}
}

rooms = main_gameplay()
# Call for the function to display instructions
show_instructions()

# variables for the current room and player moves and empty inventory list.
location = 'Great Hall'
player_move = ''
inventory = []

# gameplay loop for game
while len(inventory) < 6:
    player_location()
    player_move = input('Which direction would you like to go?\n').title()  # Ask player 
                for direction to go.
    if player_move == 'Exit':  # If Exit is selected, game over.
        location = 'Exit'
        print('Thank you for playing!')  # Thank you message for playing
    if 'Item' in rooms[get_new_location(location, player_move)]:
        print('You see a ', 'Item')
        if player_move == ('Get Item'):
            inventory = inventory.append()
    else:  # using if/else statements and dict for rooms and telling players where they 
            are.
        location = get_new_location(location, player_move)
        print(location)
        if location in rooms:
            if location == 'Main Hall':
                print()
            elif location == 'Safe Room':
                print()
            elif location == 'Bedroom':
                print()
        else:
            print("Move not valid. Please try again.")  # Error message for invalid 
                                                          direction.
        if len(inventory)==6:
            print('Congratulations! You have collected all items and defeated the 
                  dragon!') 
    exit(0)
    main_gameplay()

Errors that I am receiving:

line 61, in <module>
if 'Item' in rooms[get_new_location(location, player_move)]:
NameError: name 'rooms' is not defined

CodePudding user response:

rooms = main_gameplay() assigns whatever the function main_gameplay returns to the variable rooms. While you create a variable rooms within the function main_gameplay, the function main_gameplay does not return anything, in other words, it implicitly returns None (rooms is only in the local scope of the function main_gameplay). Thus, at the global scope, the variable rooms is None. This is why you get the error message TypeError: argument of type 'NoneType' is not iterable. Simply add return rooms at the end of your declaration of the function main_gameplay.

  • Related