Home > Software design >  Building a Text Based Game in Python, area not defined
Building a Text Based Game in Python, area not defined

Time:12-13

I'm working on a text based game in python and everything seems to be working except for the area which the player will travel to and collect items in. When I start the game, it goes through the intro (which I left out) and then shows me this error along with the player's stats:

item = player(location) enter code hereline 59, in player return area[location]['item'] NameError: name 'area' is not defined

I don't really understand how to fix it, I've been trying to define area but I'm still getting the same error.

def rules():
    print(' \n Using the commands, North, South, East and West to navigate through the warehouse.')
    print('Find your Tiramisu cake! Search the rooms of the warehouse to get your cake!')
    print('........Or use Exit to leave the game.')
    
area = {
        'Warehouse': {
            'South': 'Basement', 'North': 'Storage Room', 'East': 'Parking Lot', 'West': 'Dusty Room'
        },
    'Parking Lot': {'West': 'Warehouse','item': 'key'},
    'Basement': {'North': 'Warehouse', 'item': 'Kingpin'},
    'Storage Room': {'South': 'Warehouse', 'item' : 'Tiramisu Cake'},
    'Dusty Room': {'East': 'Warehouse', 'item': 'Safe' } 
}

location = 'Warehouse'

def fetch(location, new_dir):
    new_room = location 
    for i in area:
        if i == location:
            if new_dir in area [i]:
                new_room = area [i][new_dir]
    return new_room

def stats(location):
    return area[location]['item']

rules()
pockets = []

#loop starts here
while 1: 
    print('\n***********************************************')
    print('You are in the', location)
    print('In your pockets you hold', pockets)
    print('***********************************************')
    item = area[location].get('item')
    print('In this room you see', 'item')
    if item == 'Kingpin':
        print ('Oh no! The Kingpin!!......He pummels you, Game Over')
        exit(0)
    if item is not None and new_dir == 'You got '   item:
        if item in pockets:
            print('This room seems empty.....oh wait you already got that item')
        else:
            pockets.append
    new_dir = input('\n Which direction are you going?: ')
    new_dir = new_dir.capitalize
    if (new_dir == 'Exit'):
        print('Thanks for playing!')
        exit(0)
    if new_dir == 'North' or new_dir == 'South' or new_dir == 'East' or new_dir == 'West':
        new_room = fetch(location, new_dir)
    if new_room == location:
        print(' \n Invalid move, try another direction.')
    else:
        location = new_room
    if new_dir ==str('get'  'item'):
        if 'item' in pockets:
            print('You found', 'item', '!')
            pockets.append(area[location]['item'])
        else:
            print('Invalid move')
        if len(pockets) ==3:
            print('You have got what you came for, you win! Thanks for playing!')
            exit(0)
        break
   

CodePudding user response:

I am not really sure but by the error you have probbably not defined wich area(argument of the area ex. North)

CodePudding user response:

You haven't passed area to the fecth() and stats() functions. Either pass it or make it global

  • Related