[Python] I have a simplified text based game. Everything is working except the error message...

Discussão em 'Python' iniciado por Stack, Abril 11, 2025.

  1. Stack

    Stack Membro Participativo

    This is an assignment with requirements that the player be able to move room to room, use the string "exit" to end the game and recieve an error message when trying to move a direction that is not an exit in the current room.

    It has to be a logic error the code works for everything else.

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

    ##player instructions N,S,E,W or exit
    print('You may enter directions to move from room to room, or exit to end the game.')
    print('For each direction enter the first letter of the word.')
    print('Example, entering N will move you North.')
    print('Commands are not case sensitive.')

    ##initialize player location in start room
    player_location = 'Great Hall'

    #direction input mapping
    direction_map = {'n':'North', 's':'South', 'e': 'East', 'w':'West'}

    while True:
    ##display player location and available exits
    print(f'\nYou are currently in {player_location}')
    print('Available directions:', ', '.join([d.title() for d in rooms[player_location].keys()]))

    command = input("Enter a direction (N, S, E, W) or 'exit': ").strip().lower()



    if command == 'exit':
    print('Hope you enjoyed the game. Goodbye')
    break

    if command not in direction_map:
    print("Invalid entry. Please enter N, S, E, W, or 'exit',")
    continue

    direction = direction_map[command]


    #update players location if valid
    if direction in rooms[player_location]:
    player_location = rooms[player_location][direction]
    print(f'You moved {direction} to the {player_location}.')
    #check if the direction is available in the current room
    else:
    print('You are blocked by a wall.')


    I started in the great hall and the only valid direction to leave this room is south. To test the error message I entered north. It seems to run through the loop without displaying the error messagee. I initially set the player_location = "Great Hall". When I check this in the debugger (pycharm) it looks like it isn't checking the dictionary for exits to the great hall. I'm lost on how to fix this, and I don't want to start all over because everything else is working.

    Continue reading...

Compartilhe esta Página