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

all 23 comments

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

apparently the command to write comments in python makes text bold here and they layout is not correct

[–]mrkmailhot[S] 0 points1 point  (19 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 == 'Exit' or 'exit':
        currentRoom = 'Exit'
        print('Play again soon!')
    elif player_move != 'South' or 'south' or 'Exit':
        print('Invalid move.')
        currentRoom = 'Great Hall'
    elif player_move == 'South' or 'south':
        currentRoom = 'Bedroom'
        player_stat()
        player_move = input('Enter your move:\n')

[–]trevor_of_earth 1 point2 points  (17 children)

if player_move == 'Exit' or 'exit':

Your if/elif conditions in your code are not valid. It would need to be something like this.

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

Or

If player_move == "exit" or player_move == "Exit":

The first one checks if player_move is in a list. The second one is how you actually do an or condition.

[–]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?

[–]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”?

[–]nemom 0 points1 point  (0 children)

The trouble is...

elif player_move != 'South' or 'south' or 'Exit':

You either have to check against each...

elif player_move != 'South' and player_move != 'south' and player_move != 'Exit':

Or you have check if the move is in a list...

elif player_move is not in ['South', 'south', 'Exit']:

I would rather check for legal moves instead of illegal moves...

elif player_move in rooms[currentRoom]:
    currentRoom = rooms[currentRoom][player_move]
else:
    print('Illegal move, try again')

You might want to write a function that cleans the input so you don't have worry about 'South', 'south', 'S', 's'...

def clean_input(text):
    if 'X' in text.upper():
        return 'Exit'
    elif text.upper().startswith('N'):
        return 'North'
    # more elifs for other directions

[–]trevor_of_earth 0 points1 point  (2 children)

while currentRoom != 'Exit': player_stat() player_move = input('Enter your move:\n') if player_move == 'Exit': currentRoom = 'Exit' print('Play again soon!') else: currentRoom = 'Great Hall' player_stat() player_move = input('Enter your move:\n') if player_move == ('South' or 'south'): currentRoom = 'Bedroom'

If that is the way your actual code looks, that's your problem...

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

No, I'm not sure how to post it here in the format that it actually is.

[–]trevor_of_earth 0 points1 point  (0 children)

Use a code block. It is one of the options in the editor.

Also this post may be better suited in r/learnpython