0

I am trying to get my code to allow one of 2 valid entries from the user. It will allow the exit input, but will not allow the direction input. Instead it continues to prompt and print out Invalid entry when user enters south. I have set it as an input option, so why can I only get the code to recognize exit or to recognize south (when code is edited), but never both arguments. What am I doing wrong here?

#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'}
    }
global current_room, validate, next_room

#next statement allows top line in python to be hidden for a cleaner look while testing
print('\n' * 10)

#gives user their starting location
print('Your starting and current location is the Great Hall.\n')

#sets current location for easier room transition when user moves
current_room = 'Great Hall'

#should validate the user inputs the correct direction with set parameters
validate = {'up': 'north', 'down': 'south', 'right': 'east', 'left': 'west', 'exits': 'exit'}

#if statement prompts user for input anytime the location is great hall
if current_room == 'Great Hall':
    next_move = str(input('You can only travel south from the Great Hall. \nEnter south to 
                           continue or exit:'))

#should prompt user until validated entry is entered
#FIXME
while next_move not in validate['down' and 'exits']:
    print('\nInvalid entry.\n')
    next_move = str(input('\nEnter south or exit:'))
    continue

#FIXME
#Does not recognize south as valid entry...WHY??
if next_move in validate['down']:
    print('\nYou are now in the Bedroom.')
    current_room = 'Bedroom'

else:
    next_move in validate['exits']
    exit('\nThanks for playing. Goodbye.')
martineau
  • 119,623
  • 25
  • 170
  • 301
TammyJ
  • 1
  • 3
  • 1
    `validate['down' and 'exits']` -> `validate['exits']` -> `'exit'` – jonrsharpe Jun 08 '22 at 16:21
  • Thank you, I tried that, and if I do just 'exits', it still only recognizes exit as valid, if I do only down, it only recognizes 'south' as valid. It is the same thing if I change the 'and' to 'or', it only recognizes south or exit as valid input, and prints invalid input for at least one of the 2 choices. – TammyJ Jun 08 '22 at 16:23
  • Off-topic: Note that declaring variables `global` outside of a function has no affect whatsoever. Inside a function it prevents the creation of local variable with the specified name(s) (and allows access to the variable in the outer module-level global scope). – martineau Jun 08 '22 at 16:44

1 Answers1

0

This doesn't do what you think it does:

next_move not in validate['down' and 'exits']

'down' and 'exits' returns one value, so you're only ever checking validate['exits'].

You need to do individual checks for each value you want:

next_move not in validate['down'] and next_move not in validate['exits']

Though, in is also probably not the correct operator here. You probably wanted:

next_move != validate['down'] and next_move != validate['exits']
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • 1
    Or, if you really want to use `in`, then `next_move not in (validate['down'], validate['exits'])` – gimix Jun 08 '22 at 16:26
  • Thank you! However I tried it that way as well. It still only recognizes one of them as valid. :( – TammyJ Jun 08 '22 at 16:28
  • How are you trying it? I see that it works when I tried it. – gen_Eric Jun 08 '22 at 16:34
  • 1
    Thank you! I had tried that before, but left out the 'validate' in front of exits! lol, This fixed it! THANK YOU!!!!!!!!!!! I have been tweaking this code, trying to figure this out for hours. – TammyJ Jun 08 '22 at 16:34