Working on movement in a Python text game, but exit and error commands aren't working
Question:
I’m a total python newbie in a beginner programming class, and I’m stuck on one of our assignments. I’m supposed to be creating the bare bones start of a text game in which I move around rooms. I’ve managed to get the movement to work, but the "invalid command" and "exit game" commands aren’t working.
rooms = {
'Foyer': {'name': 'Foyer', 'South': 'Dining Hall', 'North': 'Hallway', 'West': 'Courtyard', 'East': 'Grand Hall'},
'Dining Hall': {'name': 'Dining Hall', 'South': 'Servants Quarters', 'East': 'Kitchen', 'North': 'Foyer',
'West': 'Drawing Room'},
'Servants Quarters': {'name': 'Servants Quarters', 'North': 'Dining Hall'},
'Drawing Room': {'name': 'Drawing Room', 'East': 'Dining Hall'},
'Kitchen': {'name': 'Kitchen', 'West': 'Dining Hall'},
'Courtyard': {'name': 'Courtyard', 'East': 'Foyer'},
'Grand Hall': {'name': 'Grand Hall'},
'Hallway': {'name': 'Hallway', 'South': 'Foyer', 'East': 'Private Library'},
'Private Library': {'name': 'Private Library', 'West': 'Hallway', 'East': 'Master Bedroom'},
'Master Bedroom': {'name': 'Master Bedroom', 'West': 'Private Library'}
}
directions = ['North', 'South', 'East', 'West']
player_room = rooms['Foyer']
# turn below into definition that can be called on later
while True:
print()
print('You have entered the {}.'.format(player_room['name']))
command = input('nEnter a direction: North, South, East, or West')
if command in directions:
if command in player_room:
player_room = rooms[player_room[command]]
else:
print("You face a wall. There is no doorway here.")
elif command == "Exit" or "exit":
print("Play again soon.")
break
else:
print("Type a cardinal direction to move. Directions are case-sensitive.")
This is my code so far. Whenever I test inputting an invalid command, like a number or a random word, it just gives me the ‘exit’ text and ends the program. I thought the problem was the order of the commands, so I swapped them around and messed with the statements a little, but then it would just repeatedly give me the error text even if I typed "exit". How do I stop one from always taking priority over the other?
Answers:
change your elif as below
rooms = {
'Foyer': {'name': 'Foyer', 'South': 'Dining Hall', 'North': 'Hallway', 'West': 'Courtyard', 'East': 'Grand Hall'},
'Dining Hall': {'name': 'Dining Hall', 'South': 'Servants Quarters', 'East': 'Kitchen', 'North': 'Foyer',
'West': 'Drawing Room'},
'Servants Quarters': {'name': 'Servants Quarters', 'North': 'Dining Hall'},
'Drawing Room': {'name': 'Drawing Room', 'East': 'Dining Hall'},
'Kitchen': {'name': 'Kitchen', 'West': 'Dining Hall'},
'Courtyard': {'name': 'Courtyard', 'East': 'Foyer'},
'Grand Hall': {'name': 'Grand Hall'},
'Hallway': {'name': 'Hallway', 'South': 'Foyer', 'East': 'Private Library'},
'Private Library': {'name': 'Private Library', 'West': 'Hallway', 'East': 'Master Bedroom'},
'Master Bedroom': {'name': 'Master Bedroom', 'West': 'Private Library'}
}
directions = ['North', 'South', 'East', 'West']
player_room = rooms['Foyer']
# turn below into definition that can be called on later
while True:
print()
print('You have entered the {}.'.format(player_room['name']))
command = input('nEnter a direction: North, South, East, or West').title()
if command in directions:
if command in player_room:
player_room = rooms[player_room[command]]
else:
print("You face a wall. There is no doorway here.")
elif command == "Exit" or command == "exit":
print("Play again soon.")
break
else:
print("Type a cardinal direction to move. Directions are case-sensitive.")
The issue is with the condition in your elif
statement: command == "Exit" or "exit"
. What this actually does is always evaluate to True because the string "exit"
is always considered a true value.
You can fix this by changing the condition to command == "Exit" or command == "exit"
. This way, the condition will only be True if the input is either "Exit"
or "exit"
.
You can also store the command inside a list and use the lower()
method to convert the user’s input to lowercase so that it is not case-sensitive.
I’m a total python newbie in a beginner programming class, and I’m stuck on one of our assignments. I’m supposed to be creating the bare bones start of a text game in which I move around rooms. I’ve managed to get the movement to work, but the "invalid command" and "exit game" commands aren’t working.
rooms = {
'Foyer': {'name': 'Foyer', 'South': 'Dining Hall', 'North': 'Hallway', 'West': 'Courtyard', 'East': 'Grand Hall'},
'Dining Hall': {'name': 'Dining Hall', 'South': 'Servants Quarters', 'East': 'Kitchen', 'North': 'Foyer',
'West': 'Drawing Room'},
'Servants Quarters': {'name': 'Servants Quarters', 'North': 'Dining Hall'},
'Drawing Room': {'name': 'Drawing Room', 'East': 'Dining Hall'},
'Kitchen': {'name': 'Kitchen', 'West': 'Dining Hall'},
'Courtyard': {'name': 'Courtyard', 'East': 'Foyer'},
'Grand Hall': {'name': 'Grand Hall'},
'Hallway': {'name': 'Hallway', 'South': 'Foyer', 'East': 'Private Library'},
'Private Library': {'name': 'Private Library', 'West': 'Hallway', 'East': 'Master Bedroom'},
'Master Bedroom': {'name': 'Master Bedroom', 'West': 'Private Library'}
}
directions = ['North', 'South', 'East', 'West']
player_room = rooms['Foyer']
# turn below into definition that can be called on later
while True:
print()
print('You have entered the {}.'.format(player_room['name']))
command = input('nEnter a direction: North, South, East, or West')
if command in directions:
if command in player_room:
player_room = rooms[player_room[command]]
else:
print("You face a wall. There is no doorway here.")
elif command == "Exit" or "exit":
print("Play again soon.")
break
else:
print("Type a cardinal direction to move. Directions are case-sensitive.")
This is my code so far. Whenever I test inputting an invalid command, like a number or a random word, it just gives me the ‘exit’ text and ends the program. I thought the problem was the order of the commands, so I swapped them around and messed with the statements a little, but then it would just repeatedly give me the error text even if I typed "exit". How do I stop one from always taking priority over the other?
change your elif as below
rooms = {
'Foyer': {'name': 'Foyer', 'South': 'Dining Hall', 'North': 'Hallway', 'West': 'Courtyard', 'East': 'Grand Hall'},
'Dining Hall': {'name': 'Dining Hall', 'South': 'Servants Quarters', 'East': 'Kitchen', 'North': 'Foyer',
'West': 'Drawing Room'},
'Servants Quarters': {'name': 'Servants Quarters', 'North': 'Dining Hall'},
'Drawing Room': {'name': 'Drawing Room', 'East': 'Dining Hall'},
'Kitchen': {'name': 'Kitchen', 'West': 'Dining Hall'},
'Courtyard': {'name': 'Courtyard', 'East': 'Foyer'},
'Grand Hall': {'name': 'Grand Hall'},
'Hallway': {'name': 'Hallway', 'South': 'Foyer', 'East': 'Private Library'},
'Private Library': {'name': 'Private Library', 'West': 'Hallway', 'East': 'Master Bedroom'},
'Master Bedroom': {'name': 'Master Bedroom', 'West': 'Private Library'}
}
directions = ['North', 'South', 'East', 'West']
player_room = rooms['Foyer']
# turn below into definition that can be called on later
while True:
print()
print('You have entered the {}.'.format(player_room['name']))
command = input('nEnter a direction: North, South, East, or West').title()
if command in directions:
if command in player_room:
player_room = rooms[player_room[command]]
else:
print("You face a wall. There is no doorway here.")
elif command == "Exit" or command == "exit":
print("Play again soon.")
break
else:
print("Type a cardinal direction to move. Directions are case-sensitive.")
The issue is with the condition in your elif
statement: command == "Exit" or "exit"
. What this actually does is always evaluate to True because the string "exit"
is always considered a true value.
You can fix this by changing the condition to command == "Exit" or command == "exit"
. This way, the condition will only be True if the input is either "Exit"
or "exit"
.
You can also store the command inside a list and use the lower()
method to convert the user’s input to lowercase so that it is not case-sensitive.