I'm having trouble with if statements

python

#1

Please read the following code:
I'll write down below of what I'm having trouble

# the room elements go as the following description, North, east, south, west
room_list = []

room_lobby = ["You are at the lobby", None, 8, 6, 1]
room_A = ["You are at room A", None, 0, 2, None]
room_B = ["Your are at room B", 1, None, 3, None]
room_C = ["You are at room C", 2, 4, None, None]
room_upstairs = ["You are at room D", 6, 5, None, 3]
room_E = ["You are at room E", 7, None, None, 4]
room_museum_shows = ["You are at the arena", 0, None, 4, None]
room_G = ["You are at room 7", 8, None, 5, None]
room_H = ["You are at room 8", None, None, 7, 0]

room_list.append(room_lobby)
room_list.append(room_A)
room_list.append(room_B)
room_list.append(room_C)
room_list.append(room_upstairs)
room_list.append(room_E)
room_list.append(room_museum_shows)
room_list.append(room_G)
room_list.append(room_H)

current_room = 0

done = False

while done != True:
    print room_list[current_room][0]
    choice = "k"
    if choice == "n" or "north" or "North": 
        choice = room_list[current_room][1]
        if choice == None: 
            print "You have to try again, there's no room there" 
        else: 
            done = True 
            print " " 
    elif choice == "e" or "east" or "East": 
        choice = room_list[current_room][2] 
        if choice == None: 
            print "You have to try again, there's no room there" 
        else: 
            done = True 
            print " " 
    elif choice == "s" or "south" or "South": 
        choice = room_list[current_room][3]
        if choice == None: 
            print "You have to try again, there's no room there" 
        else: 
            done = True 
            print " " 
    elif choice == "w" or "west" or "W": 
        choice = room_list[current_room][4]
        if choice == None: 
            print "You have to try again, there's no room there" 
        else: 
            done = True 
            print " " 
    else: 
        print "You don't make sense, try again"

My problem is that whatever I'm inputting in the choice or I might even put raw_input, doesn't matter, It still prints out that I have to try again, Let's say i put west, it's supposed to say done = true right? But it doesn't work...
If i put k, its supposed to print out "You dont make sense try again" but it doesn't, it says "there s no room there..."

I've tried putting into python visulaizer tutor but however the input, it doesn't check over and just prints out "There's no room there and blah blah"

Does anyone know how to fix it
it'll help alot
thanks


#2

I have formatted your code correctly. The problem is in this line:

if choice == "n" or "north" or "North":

This conditional is interpreted by Python as three different values to test for truthiness (separated by or):

  1. choice == "n"
  2. "north"
  3. "North"

Point 1 (for choice = "k") is obviously False. Now, point 2 is just the non-empty string "north", and non-empty strings in Python have a truthy value! So, since point 2 evaluates to True, the whole conditional in this line:

if choice == "n" or "north" or "North":

becomes True and thus this if branch is executed. You should instead be doing:

if choice in ["n", "north", "North"]:

using the in operator in Python with a list.

Try experimenting in repl in case you need further help

Hope it helps! :smiley:


#3

Hey
Thank you soo much
It worked !!!