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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sorry Die
    New Member
    • Mar 2012
    • 1

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

    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.
    Code:
    def chooseEntryway():
        room = ''
        print('Which entryway will you take? The window, the back door, or the front door?')
        room = input()
        room = str(room)
        if room != 'window' or 'the back door' or 'the front door' or 'the window' or 'front door' or 'back door':
            print('That is not an available entryway. You need to enter from the window, the front door, or the back door.')
        while room == '':
            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?
    Last edited by bvdet; Mar 4 '12, 05:27 PM. Reason: Please use code tags when posting code
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    Code:
    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)
    Code:
    room="window"
    
    if room != 'window':
        print "room not equal window"
    # or 'the back door'
    elif 'the back door':  ## always true
        print "the back door"
    elif 'the front door':  ## always true
        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.

    Comment

    Working...