0

I'm just wondering why the loop doesn't break when it meets those conditions and filters over into my other functions? I fixed it by doing a while true loop and just breaking in each if statement, but I'd like to know what is wrong with doing this way.

def main_entrance():

print "\n\tYou are in the main entrance. It is a large room with" 
print "\ttwo doors, one to the left and one to the right. There"
print "\tis also a large windy stair case leading up to a second floor."
print "\n\tWhat are you going to do?\n"
print "\t #1 take the door on the left?"
print "\t #2 take the door on the right?"
print "\t #3 take the stairs to the second floor?"

choice = 0

#This seems to be the part that isn't working as I would expect it to.
# I have fixed it and have commented the fix out so that I can understand
# why this way isn't working.

#while True:

while (choice != 1) or (choice != 2) or (choice != 3):


    try:
        choice = int (raw_input ('> '))
        if (choice == 1):
            door_one_dinning_room()
            #break (should not need this break if choice is == 1, 2, 3)

        elif (choice == 2):
            door_two_study()
            #break

        elif (choice == 3):
            stairs_to_landing() 
            #there isn't anything in this function
            #but rather than breaking out from the program once it is 
            # called, but somehow this while loop is still running.

            #break

        else:
            print "You must pick one!"

    except:
        print "Please pick a number from 1-3"
        continue

2 Answers2

10

Of course it doesn't break, your condition can never be false

(choice != 1) or (choice != 2) or (choice != 3)

Think about it for a minute, any selection of choice cannot make this expression false.

choice = 1

False or True or True --> True

choice = 2

True or False or True --> True

choice = 3

True or True or False --> True

Solution

You need to and the conditions together

(choice != 1) and (choice != 2) and (choice != 3)

Or better yet

while choice not in [1,2,3]
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • well explained and concise solution! I like this answer a lot! – Joe Smart Sep 22 '14 at 13:53
  • Damnit, why didn't I notice that! gah, feel stupid now. LOL.. yeah my logic was way off. New to programming.. Thanks so much all makes perfect sense! – Daniel Richardson Sep 22 '14 at 14:03
  • Use {1,2,3} instead of [1,2,3]. Set lookups are more efficient than list lookups. Depending on what version of Python you are using, the literal set (or list) can be created at compile time rather than run time, which would emphasize the speed up. – chepner Sep 22 '14 at 14:24
5
while (choice != 1) or (choice != 2) or (choice != 3):

This condition is always true. If your choice variable equals 1, then choice!=1 is false, but choice!=2 is true, so the whole condition is true. That is what or means.

You could put:

while (choice != 1) and (choice != 2) and (choice != 3):

or more succinctly:

while choice not in (1,2,3):
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • Use `{1,2,3}` instead of `(1,2,3)`. Set lookups are more efficient than tuple lookups. Depending on what version of Python you are using, the literal set (or tuple) can be created at compile time rather than run time, which would emphasize the speed up. – chepner Sep 22 '14 at 14:23