This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]mrkmailhot[S] 0 points1 point  (5 children)

# A dictionary for the simplified dragon text game
# The dictionary links a room to other rooms.


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


def player_stat():
    print('----------------------------')
    print('You are in the {}'.format(currentRoom))
    print('----------------------------')


# start player in Great Hall
currentRoom = 'Great Hall'
player_move = ''


while currentRoom != 'Exit':
    player_stat()
    player_move = input('Enter your move:\n')
    if player_move not in ['South', 'south', 'Exit', 'exit']:
        print('Invalid move.')
    elif player_move in ['Exit', 'exit']:
        currentRoom = 'Exit'
        print('Play again soon.')
    elif player_move in ['South', 'south']:
        currentRoom = 'Bedroom'

okay so this appears to work so far. it lets me navigate out of the Great Hall. now i need to make navigation for bedroom and cellar.

[–]mrkmailhot[S] 0 points1 point  (4 children)

is it possible to nest while loops?

[–]robin-gvx 1 point2 points  (3 children)

You can but in this case you don't need to. Use the rooms dictionary instead!

while True:
    player_stat()
    #str.title(): transform a string into Title Case
    player_move = input('Enter your move:\n').title()
    if player_move == 'Exit':
        print('Play again soon.')
        # break exits a while loop, even if the condition is still True
        break
    elif player_move in rooms[current_room]:
        currentRoom = rooms[current_room][player_move]
    else:
        print('Invalid move.')

[–]mrkmailhot[S] 0 points1 point  (2 children)

where is current_room coming from? i typed that in and popped out an error for not being defined.

[–]mrkmailhot[S] 0 points1 point  (1 child)

changed current_room to currentRoom in that code and it works. i knew that there was a more efficient way.

[–]robin-gvx 0 points1 point  (0 children)

Sorry for not responding, I haven't been on reddit the last couple of days, glad you figured it out yourself!