ked730
ked730

Reputation: 3

How do I get an invalid message to print?

I am making a text based game for an assignment where you move room to room. I have my code working thus far, however I am having trouble getting a message to pop up if the move is invalid. Can anyone guide me in the right direction? Im aware of the print at the end of the code. That is the message I need pop up if the move is invalid..hope that makes sense. I am very new to coding.

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

startingbiome = 'Great Hall'  # starting room

currentbiome = startingbiome

while True:

    print('You are in the', currentbiome)  # tells user their current location

    swim = input('Which direction would you like to go? Or would you like to exit?')
    if swim == 'Exit':
        break
    if swim:
        currentbiome = rooms[currentbiome][swim]
    
    
        print('You cant go that way! Please choose a new way to swim!')

Upvotes: 0

Views: 72

Answers (1)

Barmar
Barmar

Reputation: 782785

Use the in operator to test if the input is a key of the dictionary.

if swim.lower() == 'exit':
    break
if swim in rooms[currentbiome]:
    currentbiome = rooms[currentbiome][room]
else:
    print("You can't go that way!")

Upvotes: 2

Related Questions