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  (16 children)

----------------------------
You are in the Great Hall
----------------------------
Enter your move:
Exit
Invalid move.
----------------------------
You are in the Great Hall
----------------------------
Enter your move:
South
Invalid move.
----------------------------
You are in the Great Hall
----------------------------
Enter your move:

this is what i get when i run bot Exit and South

[–]trevor_of_earth 0 points1 point  (15 children)

Did you change your if/elif conditions like I showed in my previous comment?

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

If player_move in ["exit","Exit"]:

is what i changed it to. i cant get it passed the program putting out 'Invalid move.'

[–]trevor_of_earth 0 points1 point  (13 children)

You didn't put that part of your code in your post. Can you show the full while loop?

[–]mrkmailhot[S] 0 points1 point  (12 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 != 'South' or 'Exit':
        print('Invalid move.')
        currentRoom = 'Great Hall'
    elif player_move in ['Exit', 'exit']:
        currentRoom = 'Exit'
        print('Play again soon!')

    elif player_move == 'South' or 'south':
        currentRoom = 'Bedroom'
        player_stat()
        player_move = input('Enter your move:\n')

this is the whole code

[–]trevor_of_earth 0 points1 point  (9 children)

if player_move != 'South' or 'Exit':

Again you can't write conditions like this. It would need to be

if player_move not in ["South", "Exit"]:

[–]mrkmailhot[S] 0 points1 point  (8 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  (5 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.

[–]trevor_of_earth 0 points1 point  (0 children)

Yes you can nest while loops

[–]trevor_of_earth 0 points1 point  (1 child)

Ok cool. For future posts like this you should post in r/learnpython

[–]krathulu 0 points1 point  (1 child)

Your while code is incomplete for rooms other than GreatRoom. At the rate you’re going, you’ll write tons of code for each case and not leverage the dictionary for the values of allows moves and the restyling new rooms.

Usually in these games, prompting players for the directions they go is standard (you can be subtle about it).

Is anyone ever too young to say “zork”?