This is my code and I am trying to move from one area to another using the move_player() function. The code is not changing the current_location variable and I have no idea why. Please help me
#The dictionary of areas which links the areas together along with the item in each area
areas = {
'geysers':{'north':'caves', 'south':'fields', 'east':'swamps', 'west':'treetops', 'item':'start'},
'caves':{'south':'geysers', 'east':'cliffs', 'item':'sword'},
'fields':{'north':'geysers', 'east':'temple', 'item':'bandages'},
'swamps':{'north':'pond', 'west':'geysers', 'item':'tranquilizer'},
'treetops':{'east':'geysers', 'item':'blowtorch'},
'cliffs':{'west':'caves', 'item':'shield'},
'pond':{'south':'swamps', 'item':'javelin'},
'temple':{'west':'fields', 'item':'villain'}
}
current_location = 'geysers'
#function to handle invalid entries for moving the player
def invalid_move():
print('Move not valid. Try again.')
#get the player's next move and put it in a list for validation
player_input = input('Enter your move:\n>').lower().split()
def move_player(location, player_move):
new_location = location
if player_move[0] == 'go':
if player_move[1] in areas[location] and player_move[1] != 'item':
new_location = areas[location][player_move[1]]
else:
invalid_move()
else:
invalid_move()
return new_location
move_player(current_location, player_input)
print(current_location)
Note that your move_player
function return
s a value. When you called the move_player
function, you did not assign the returned value to any variable, hence the function call did nothing. In order to have the returned value reflect in the current_location
variable, replace this line:
move_player(current_location, player_input)
with:
current_location = move_player(current_location, player_input)