By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,571 Members | 960 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,571 IT Pros & Developers. It's quick & easy.

I don't understand why this isn't working, help?

P: 1
So, I'm learning how to program in Python, and so far I think I'm doing okay. While trying a SWAT type of game, I made this.
Expand|Select|Wrap|Line Numbers
  1. def chooseEntryway():
  2.     room = ''
  3.     print('Which entryway will you take? The window, the back door, or the front door?')
  4.     room = input()
  5.     room = str(room)
  6.     if room != 'window' or 'the back door' or 'the front door' or 'the window' or 'front door' or 'back door':
  7.         print('That is not an available entryway. You need to enter from the window, the front door, or the back door.')
  8.     while room == '':
  9.         chooseEntryway()
This apparently doesn't work, even when Room DOES equal one of those, it still thinks it's false and prints the last line. Anyone know what's wrong?
Mar 4 '12 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 626
Expand|Select|Wrap|Line Numbers
  1. if room != 'window' or 'the back door' or 'the front door' or 'the window' or 'front door' or 'back door':
You have 2 problems. If room contains "the back door" it will not equal 'window', so there will always be 5 strings that do not match it. Second, the if statement breaks down into (truncated for brevity)
Expand|Select|Wrap|Line Numbers
  1. room="window"
  2.  
  3. if room != 'window':
  4.     print "room not equal window"
  5. # or 'the back door'
  6. elif 'the back door':  ## always true
  7.     print "the back door"
  8. elif 'the front door':  ## always true
  9.     print "the front door" 
Generally you search a list, or not in a list, when you have multiple items. See this page on Searching Lists using the "in" operator.
Mar 4 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.