Ex36, While loop

I’m working on creating my game in learn lp3thw ex36 and am trying to use a while loop, but am having some trouble. I get that this is a lot to read through, but would appreciate some help if possible. Here is my code:

def first_room():
    print("Pick an item out of this floating hat.")
    import random
    items = ["candy cane", "stuffed spider", "rubber snake"]
    global your_item
    your_item = random.choice(items)
    print(f"You got a {your_item}")
    print(f"What would you like to do now? Option 1: Stay here with your {your_item}, or\nOption 2: Go through the next door? ")
    first_room_choice()

def first_room_choice():
    choice = int(input("1 or 2>"))
    print(choice)
    
    while choice != 1 or 2:

        if your_item is "candy cane" and choice is 2:
            print("yum! I'm taking you through door number one.")
            candy_room()

        elif your_item is "stuffed spider" and choice is 2:
            print("Yikes! I'm taking you through door number two.")
            spider_room()

        elif your_item is "rubber snake" and choice is 2:
            print("Yikes! I'm taking you through door number three.")
            snake_room()

        elif choice is 1:
            print("You eventually starve.")
            dead("You chose to stay in the First Room.")

        else:
            print("That's not a '1' or a '2'.")
            first_room_choice()

When I input “1” it works fine, when I input “3” it works fine (although I’m suspicious that it is working for the right reason) , but when I input “2” the else statement at the end runs instead of one of the first three if statements.

Remember computers are dumb and need to be told things explicitly, unlike Humans.

Try:

while choice != 1 or choice != 2:

Or if you only want one choice statement you could use a range function but it might be overkill for a range of 1-2 inclusively.

However your while loop run while a condition is true. Your condition is negative (while choice doesn’t equal 1 or 2) but you I’d statements expect 1 or 2 as a condition. That’s going to give you problems.

1 Like

Thanks @gpkesley. I didn’t end up going with the while loop because I couldn’t figure out how to do it without a negative condition. You did, however, point out some mistakes I had no matter how I was going to structure this and also some areas I where I need more practice. I ended up making it work like this:

    import random
    items = ["candy cane", "stuffed spider", "rubber snake"]
    global your_item
    your_item = random.choice(items)
    print(f"You got a {your_item}")
    print(f"What would you like to do now? Option 1: Stay here with your {your_item}, or\nOption 2: Go through the next door? ")
    first_room_choice()

def first_room_choice():
    choice = int(input("1 or 2>"))
    if choice == 1 or choice == 2:

        if choice == 2:
            if your_item == "candy cane":
                print("yum! I'm taking you through door number one.")
                candy_room()

            elif your_item == "stuffed spider":
                print("Yikes! I'm taking you through door number two.")
                spider_room()

            else:
                print("Yikes! I'm taking you through door number three.")
                snake_room()

        else:
            print("You eventually starve.")
            dead("You chose to stay in the First Room.")

    else:
        print("That's not a '1' or a '2'.")
        first_room_choice()

This probably isn’t the most elegant way of doing this, but I’m happy its working.

1 Like

@andreotte good work. @zedshaw has a phrase for it: result oriented programming. First get it done and running, then make it better later. I incorporatet this simple phrase in a lot of areas in my life. It works wonder!

1 Like