Python if/elif/else


#1



All of the code works except when i input door equals 1 and input some other option other than what is given, it runs two sets of else blocks instead of one.

I'm supposed to get:

You enter a dark room with two doors. Do you go through door #1 or door #2?

1
There is a fat walrus eating lime jello. What do you do?
1. Take the jello.
2. Throw a rock at the walrus.
apples
Well, doing apples is probably better. The walrus runs away.
Press enter to exit.

I am not supposed to get this:
You enter a dark room with two doors. Do you go through door #1 or door #2?

1
There is a fat walrus eating lime jello. What do you do?
1. Take the jello.
2. Throw a rock at the walrus.
apples
Well, doing apples is probably better. The walrus runs away.
You trip over a rock and fall into a pool of lava. Good job!
Press enter to exit.

Here is my code:


Replace this line with your code. 

print("You enter a dark room with two doors. Do you go through door #1 or door #2?")

door = input()

if door == "1":
    print("There is a fat walrus eating lime jello. What do you do?")
    print("1. Take the jello.")
    print("2. Throw a rock at the walrus.")

    fear = input()

    if fear == "1":
        print("The walrus gores you in the face with his tusks. Good job!")
    elif fear == "2":
        print("The walrus rolls on top of you and squishes you. Good job!")
    else:
        print("Well, doing %s is probably better. The walrus runs away." %fear)
   
if door == "2":
    print("You look into the endless abyss of Thor's navel.")
    print("1. Purple elephant fins.")
    print("2. kumquats.")
    print("3. Smelling your own ear.")

    insanity = input()

    if insanity == "1" or insanity == "2":
        print("You feel your body melt off your spinal column. Good job!")
    elif insanity == "3":
        print("You survive, but as a toadstool forever gathering lint. Good job!")
else:
        print("You trip over a rock and fall into a pool of lava. Good job!")

input("\n\nPress the enter key to exit.")


#2

Try backspacing some white-space so that the print in this line is lined up with the:

So it looks like:

if door == "2":
    print("You look into the endless abyss of Thor's navel.")
    print("1. Purple elephant fins.")
    print("2. kumquats.")
    print("3. Smelling your own ear.")

    insanity = input()

    if insanity == "1" or insanity == "2":
        print("You feel your body melt off your spinal column. Good job!")
    elif insanity == "3":
        print("You survive, but as a toadstool forever gathering lint. Good job!")
else:
    print("You trip over a rock and fall into a pool of lava. Good job!")

I have a feeling it's the over-indention of this line that is causing the problem. The 'else' in door == 1 is lined up perfectly with the print statement in question. Python is very specific about white-space and indenting.

Hope this helps! If not, feel free to reply and I'll try to go more in-depth.


#3

It is still doing the same thing. Here is my new code:

print("You enter a dark room with two doors. Do you go through door #1 or door #2?")

door = input()

if door == "1":
print("There is a fat walrus eating lime jello. What do you do?")
print("1. Take the jello.")
print("2. Throw a rock at the walrus.")

fear = input()

if fear == "1":
    print("The walrus gores you in the face with his tusks. Good job!")
elif fear == "2":
    print("The walrus rolls on top of you and squishes you. Good job!")
else:
    print("Well, doing %s is probably better. The walrus runs away." %fear)

if door == "2":
print("You look into the endless abyss of Thor's navel.")
print("1. Purple elephant fins.")
print("2. kumquats.")
print("3. Smelling your own ear.")

insanity = input()

if insanity == "1" or insanity == "2":
    print("You feel your body melt off your spinal column. Good job!")
elif insanity == "3":
    print("You survive, but as a toadstool forever gathering lint. Good job!")

else:
print("You trip over a rock and fall into a pool of lava. Good job!")

input("\n\nPress the enter key to exit.")


#4

It might be the formatting of the post is off but now it looks like some of the print lines are out of the if-statements entirely. To format, highlight the code when making a post and either press the button ( < / > ) or use the shortcut (ctrl+shift+c). Here's a post that goes deeper into this: How to format code


#5

Ok I will wrap the code with three backticks. Here is my code:

print("You enter a dark room with two doors. Do you go through door #1 or door #2?")

door = input()

if door == "1":
    print("There is a fat walrus eating lime jello. What do you do?")
    print("1. Take the jello.")
    print("2. Throw a rock at the walrus.")

    fear = input()

    if fear == "1":
        print("The walrus gores you in the face with his tusks. Good job!")
    elif fear == "2":
        print("The walrus rolls on top of you and squishes you. Good job!")
    else:
        print("Well, doing %s is probably better. The walrus runs away." %fear)
   
if door == "2":
    print("You look into the endless abyss of Thor's navel.")
    print("1. Purple elephant fins.")
    print("2. kumquats.")
    print("3. Smelling your own ear.")

    insanity = input()

    if insanity == "1" or insanity == "2":
        print("You feel your body melt off your spinal column. Good job!")
    elif insanity == "3":
        print("You survive, but as a toadstool forever gathering lint. Good job!")
else:
    print("You trip over a rock and fall into a pool of lava. Good job!")

input("\n\nPress the enter key to exit.")

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.