I am a beginner trying to code a simple text based game where the user moves between different rooms, picks up an item from each and finishes with a boss encounter. Things seem to be going well except when I try to call a function status() inside my main function. I keep getting the error that the variables aren't defined. Any help will be appreciated.
# Create a function for the opening menu
def show_menu():
print('You are about to play a text based game.\n')
print('The goal of the game is to collect all seven items before encountering the ghost.\n')
print('These items contain information about the ghost that can finally put them to rest.\n')
print('If you approach the ghost without all of the items you will be possessed and the game will end.\n')
print('The commands are: move North, move South, move East, move West, menu, and quit.\n')
print('When you find an item, to add it to your inventory enter: get Item')
# Create a function that outputs current room, current room inventory and users current inventory
def status():
print('You are currently in the: {}.'.format(current_room))
print('The item currently in this room is {}.'.format(rooms[current_room]['item]']))
print('Your current inventory is: {}.'.format(inventory))
control = input('What would you like to do? move North, move South, move East, move West, menu, or quit.')
return control
def movement_error():
print('You look around and realize that you wanted to move through a wall, try a different direction.')
control = input('What would you like to do? move North, move South, move East, move West, menu, or quit.')
return control
def main():
# Create Dictionary linking rooms and items
rooms = {
'Foyer': {'East': 'Office', 'South': 'Cafeteria', 'item': 'Newspaper Article'},
'Office': {'West': 'Foyer', 'item': 'Student Report'},
'Cafeteria': {'North': 'Foyer', 'West': 'English Classroom', 'South': 'Math Classroom', 'East': 'Band Room',
'item': 'Crumpled Paper'},
'English Classroom': {'East': 'Cafeteria', 'item': 'Student Journal'},
'Math Classroom': {'East': 'Girls Bathroom', 'item': 'Itinerary'},
'Girls Bathroom': {'West': 'Math Classroom', 'item': 'Notebook'},
'Band Room': {'East': 'Cafeteria', 'North': 'Basement', 'item': 'Picture'},
'Basement': {'South': 'Band Room'}, # Ghost Room
}
directions = ['North', 'East', 'South', 'West']
control = ''
current_room = 'Foyer'
while control != 'exit':
inventory = []
while current_room != ['Basement']:
status()
if control in directions and contol in current_room:
current_room = rooms[current_room][control]
CodePudding user response:
Those names are only visible within main
. Pass them to your function:
def status(rooms, current_room, inventory):
# ...
to use:
control = status(rooms, current_room, inventory)
Note the assignment to control
. At present, the value returned by your status
function is unused.
CodePudding user response:
You can also declare the variables as global
in your main function:
def main():
global current_room
global inventory
global rooms
...