0
def nextroom(currentloc, direction):
    rooms = {
        'Bedroom': {'North': 'Parents Office', 'South': 'Living-room', 'East': 'Kitchen', 'West': 'Bathroom', 'Item': 'Invalid'},
        'Parents Office': {'North': 'Invalid','South': 'Bedroom', 'East': 'Dads Man Cave', 'West': 'Invalid', 'Item': 'Golf Club'},
        'Bathroom': {'North': 'invalid', 'South': 'Invalid', 'East': 'Bedroom', 'West': 'Invalid', 'Item': 'Hand Soap'},
        'Living-room': {'North': 'Bedroom', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Invalid', 'Item': 'Nerf Gun'},
        'Dining-room': {'North': 'invalid', 'South': 'Invalid', 'East': 'Invalid',  'West': 'Living-room', 'Item': 'Bucket of Marbles'},
        'Kitchen': {'North': 'Garage', 'South': 'Invalid', 'East': 'Invalid',   'West': 'Bedroom', 'Item': 'Handcuffs' },
        'Garage': {'North': 'invalid', 'South': 'Kitchen', 'East': 'Invalid', 'West': 'Invalid', 'Item': 'Rc Car'},
        'Dads Man Cave': {'North': 'invalid', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Parents Office', 'Item': 'Infiltrator'}
}
    return rooms[currentloc][direction]


def player_stat(location):
    rooms = {
        'Bedroom': {'North': 'Parents Office', 'South': 'Living-room', 'East': 'Kitchen', 'West': 'Bathroom', 'Item': 'Invalid'},
        'Parents Office': {'North': 'Invalid', 'South': 'Bedroom', 'East': 'Dads Man Cave', 'West': 'Invalid','Item': 'Golf Club'},
        'Bathroom': {'North': 'invalid', 'South': 'Invalid', 'East': 'Bedroom', 'West': 'Invalid', 'Item': 'Hand Soap'},
        'Living-room': {'North': 'Bedroom', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Invalid', 'Item': 'Nerf Gun'},
        'Dining-room': {'North': 'invalid', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Living-room', 'Item': 'Bucket of Marbles'},
        'Kitchen': {'North': 'Garage', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Bedroom', 'Item': 'Handcuffs'},
        'Garage': {'North': 'invalid', 'South': 'Kitchen', 'East': 'Invalid', 'West': 'Invalid', 'Item': 'Rc Car'},
        'Dads Man Cave': {'North': 'invalid', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Parents Office', 'Item': 'Infiltrator'}
}

    print('----------------------------')
    print('You are in {} with {}'.format(location, rooms[location]['Item']))
    print('----------------------------')


def play():
     currentRoom = 'Bedroom'
     while currentRoom != 'Exit' or 'exit':
         player_move = input('Enter your move:\n')
         if player_move == 'Exit' or player_move == 'exit':
            currentRoom = 'Exit'
            print('Play again soon!')
         elif player_move == 'South' or player_move == 'south':
            currentRoom = nextroom(currentRoom, 'South')
            player_stat(currentRoom)
         elif player_move == 'North' or player_move == 'north':
            currentRoom = nextroom(currentRoom, 'North')
            player_stat(currentRoom)
         elif player_move == 'West' or player_move == 'west':
            currentRoom = nextroom(currentRoom, 'West')
            player_stat(currentRoom)
         elif player_move == 'East' or player_move == 'east':
            currentRoom = nextroom(currentRoom, 'East')
            player_stat(currentRoom)

Above is part of coding from text based game I am creating. I am having trouble defining the "invalid" values in the rooms dictionary in my while loop. When invalid rooms are accessed by user I want it to print a str.

Thank you

below is the error I get from my side when I try to access a room ('North' : 'Invalid')

Traceback (most recent call last):
  File "/Users/jacenduplain/PycharmProjects/pythonProject3/1.py", line     111, in <module>
    play()
  File "/Users/jacenduplain/PycharmProjects/pythonProject3/1.py", line     91, in play
    player_stat(currentRoom)
  File "/Users/jacenduplain/PycharmProjects/pythonProject3/1.py", line     75, in player_stat
    print('You are in {} with {}'.format(location, rooms[location].   ['Item']))
KeyError: 'Invalid'

1 Answers1

0

Change nextroom() to check whether it's an invalid room. If it is, it can print a message and return the original room.

def nextroom(currentloc, direction):
    rooms = {
        'Bedroom': {'North': 'Parents Office', 'South': 'Living-room', 'East': 'Kitchen', 'West': 'Bathroom', 'Item': 'Invalid'},
        'Parents Office': {'North': 'Invalid','South': 'Bedroom', 'East': 'Dads Man Cave', 'West': 'Invalid', 'Item': 'Golf Club'},
        'Bathroom': {'North': 'invalid', 'South': 'Invalid', 'East': 'Bedroom', 'West': 'Invalid', 'Item': 'Hand Soap'},
        'Living-room': {'North': 'Bedroom', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Invalid', 'Item': 'Nerf Gun'},
        'Dining-room': {'North': 'invalid', 'South': 'Invalid', 'East': 'Invalid',  'West': 'Living-room', 'Item': 'Bucket of Marbles'},
        'Kitchen': {'North': 'Garage', 'South': 'Invalid', 'East': 'Invalid',   'West': 'Bedroom', 'Item': 'Handcuffs' },
        'Garage': {'North': 'invalid', 'South': 'Kitchen', 'East': 'Invalid', 'West': 'Invalid', 'Item': 'Rc Car'},
        'Dads Man Cave': {'North': 'invalid', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Parents Office', 'Item': 'Infiltrator'}
    }
    next = rooms[currentloc][direction]
    if next.lower() == 'invalid':
        print("You can't go that direction!")
        return currentloc
    else:
        return next
Barmar
  • 741,623
  • 53
  • 500
  • 612