I am writing a simple text-based game for a college course in which I need to be able to move between rooms until the exit criteria are met. The problem I am running into is I can't get the ELIF statement to work to set my location from the bedroom to the cellar. It keeps it as bedroom. I am not sure what I'm missing here.
rooms = {
'Great Hall': {'South': 'Bedroom'},
'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
'Cellar': {'West': 'Bedroom'}
}
location = 'Great Hall'
direction = ""
while direction != exit:
print("\nYou are in the", location)
possible_moves = rooms[location].keys()
print("Possible moves:", *possible_moves)
direction = input("Which direction do you wish to go? ").strip().capitalize()
print("You entered:", direction)
if direction in possible_moves:
if location == 'Great Hall':
location = 'Bedroom'
if location == 'Bedroom':
if direction == 'North':
location = 'Great Hall'
elif direction == 'East':
location = 'Cellar'
if location == 'Cellar':
location = 'Bedroom'
elif direction is not possible_moves:
if direction == 'Exit':
print("Thank you for playing the game.")
break
else:
print('Invalid direction.')
CodePudding user response:
The issue is that when you change location from Bedroom to Cellar you straight away change it back again, if location == 'Cellar'
matches, you need to change a couple of your ifs to elif
if direction in possible_moves:
if location == 'Great Hall':
location = 'Bedroom'
elif location == 'Bedroom': # if -> elif
if direction == 'North':
location = 'Great Hall'
elif direction == 'East':
location = 'Cellar'
elif location == 'Cellar': # if -> elif
location = 'Bedroom'
CodePudding user response:
the elif
is asking whether or not direction is possible_moves, instead of is in possible moves.