samedi 4 décembre 2021

Moving Between Rooms

The code I have is as follows:

rooms = {
    'Great Hall': {'South': 'Bedroom'},
    'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
    'Cellar': {'West': 'Bedroom'}
}

def show_instructions():
    print('The instructions are as follows: go north, go south, go east, go west')

def move_rooms(direction, room='Great Hall'):
    if direction == 'go south':
        room = 'Bedroom'
        return rooms[room]['South']
    elif direction == 'go north':
        if room == 'Great Hall' or 'Cellar':
            return 'Invalid direction please try again.'
        return rooms[room]['North']
    elif direction == 'go east':
        if room == 'Great Hall' or 'Cellar':
            return 'Invalid direction please try again.'
        return rooms[room]['East']
    elif direction == 'go west':
        if room == 'Bedroom' or 'Great Hall':
            return 'Invalid direction please try again.'
        return rooms[room]['West']





currentRoom = 'Great Hall'
gameInstructions = 'The instructions are as follows: go north, go south, go east, go west'
print(gameInstructions)
print(currentRoom)

userDirection = ''
while userDirection != 'exit':
    userDirection = input("Pick a direction, or type exit to exit the game.")
    if currentRoom == 'Great Hall':
        if userDirection == 'go south':
            currentRoom = move_rooms(userDirection, currentRoom)
            show_instructions()
            print(currentRoom)
    else:
        print('Invalid direction. Please pick another direction.')
        print(currentRoom)
        show_instructions()
elif currentRoom == 'Bedroom':
    if userDirection != 'go north' or 'go east':
        print('Invalid direction. Please pick another direction.')
        print(currentRoom)
        show_instructions()
    elif userDirection == 'go north':
        currentRoom = move_rooms(userDirection, currentRoom)
        print(currentRoom)
        show_instructions()
    elif userDirection == 'go east':
        currentRoom = move_rooms(userDirection, currentRoom)
        print(currentRoom)
        show_instructions()
elif 'Cellar' == currentRoom:
    if userDirection != 'go west':
        print('Invalid direction. Please pick another direction.')
        print(currentRoom)
        show_instructions()
    else:
        currentRoom = move_rooms(userDirection, currentRoom)
        print(currentRoom)
        show_instructions()
else:
    if userDirection == 'exit':
        print('Thanks for playing the game!')
        break

This is a small part of a larger text based game that I am supposed to develop as part of my college class. The goal of this program is to get user input to move between rooms. I used the template dictionary given and am trying to restrict inputs to the instruction list with the exception of the word 'exit'. So the major issue I am having is how to use the return function properly in the function I created to yield the correct result. It is giving me a 'KeyError' of 'South' in the console everytime I try to input go south as the first direction. Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire