Subject:Computer SciencePrice: Bought3
Code works. I am trying to figure out where to add an if or elif statement when the player moves in an invalid direction. Can someone please help me with the code?
def intro(): print() # Welcome the user to the game print('Welcome to the Dragon Text Based Adventure Game!') intro() # 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'}}] # Start loop while True: start_game = input('Do you want to play? Yes/No: ').lower() print() if start_game == 'no': print('See you later') print('-----------------------------------') elif start_game == 'yes': prev_room = 'Great Hall' print('You are in the Great Hall!') print('Which way would you like to move? North, East, South, West or Exit? ') print('--------------------------------------------------------------------') while True: direction = input() if direction == "Exit" or direction == "exit": print('Good Bye!') exit() try: cur_room = rooms[0][prev_room][direction.capitalize()] except KeyError: print() continue try: cur_room = rooms[0][prev_room][direction.capitalize()] if cur_room == "Exit": print("Game Over!") break print('You are in ' + cur_room) print('Which way do you want to move? North, East, South, West or Exit? ') print('-----------------------------------------------------------------') except KeyError: pass prev_room = cur_room else: print('See you later')