Python if/else/elif


#1



There is a syntax error that keeps popping up. I assume it has to do with indentation. 

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)

elif 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!")
else:
    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("Press enter to exit.")


#2

elif can only be after if or another elif, its currently after else. this can't be


#3

so do i eliminate the the line:

else:
print("Well, doing %s is probably better. The walrus runs away." %fear)


#4

we are talking about this line:

elif door == "2":

wouldn't it make more sense to change elif to if? Depending on how your program should run


#5

when i change it to if then i get another error on the second to last line:

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


#6

well, same thing apply for else. Else can only be after if or elif. You have two else's in a row


#7

I cannot find the right balance of if/elif/ and else! Logical statements are so difficult for me. I just want this code to work. Im sorry, but this is frustrating.


#8

well, the logic is pretty simply:

if condition:
   print "do something"
elif another condition:
   print "do something for other conditions"
else:
   print "do something if none of the conditions is met"

its always if, then zero or more elif conditions and finally a else for scenarios not caught by your conditions.


#9

The problem seems to be that this block is not indented to be nested inside the 'if door == "1":'
such as:

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)

elif door == "2":

Here, the nested 'if' (fear == 1) is only looked at if the first 'if' (door == 1) is true. Otherwise, it moves on to the second 'if' (door == 2) and completely ignores the nested 'if' (fear == 1).
The same thing applies to the insanity section.
I like to think of indenting as priority; the further left it is, the more important.

Hope this helps!


#10

Thank you so much for the help! Much appreciated!


#11

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