0

I'm writing a program as an exercise for if-elif-else statements and I could do with some advice as to why an elif statement is not running.

I want to give the option to pick up rocks for the inventory. That works, but if I say I do NOT want to pick up the rocks, that elif statement seems to be ignored and I pick up the rocks and they are added to my inventory.

I have no actual errors, but cannot see why the elif statement is not being actined. I have used this several times in the same program as part of this exercise and not had this continual problem.

These are the statements I seem to be having issues with:

elif num >=2 and rocks_there == True:
        print("""You keep within touching distance of the castle wall.
                You come across some rocks. Do you want to pick them up.""")
        take = input("> ").lower()

        if "yes" or "y" in take:
            print("You pick up the rocks.")
            inventory.append('Rocks')
            rocks_there = False
            print("You follow the castle walls around to the front of the castle again.")
            grounds()

        elif "no" or "n" in take:
            inventory = inventory
            rocks_there = True
            print("You leave the rocks on the path.")
            print("You follow the castle walls around to the front of the castle.")
            grounds()

Here is the whole of the function:

def fog(): global inventory global rocks_there print("You walk along the path to the left of the castle when a thick fog starts swirling around you.") print("Do you want to CARRY on or go BACK to the courtyard?")

choice = input("> ").lower()

if "back" in choice:
    grounds()

elif "carry" in choice:
    
    num = random.randint(0,10)
    if num < 2:
        print("""The fog envelopes you until you have no idea where you are and can see nothing else.
                Suddenly you feel yourself fall as you step over the edge of a towering cliff.
                You die, but at least you are not undead.""")
        exit(0)
    elif num >=2 and rocks_there == True:
        print("""You keep within touching distance of the castle wall.
                You come across some rocks. Do you want to pick them up.""")
        take = input("> ").lower()

        if "yes" or "y" in take:
            print("You pick up the rocks.")
            inventory.append('Rocks')
            rocks_there = False
            print("You follow the castle walls around to the front of the castle again.")
            grounds()

        elif "no" or "n" in take:
            inventory = inventory
            rocks_there = True
            print("You leave the rocks on the path.")
            print("You follow the castle walls around to the front of the castle.")
            grounds()

    elif num >= 2 and rocks_there == False:
        print("You follow the castle walls around to the front of the castle.")
else:
    print("The fog has scrambled your brain and I do not understand you.")
    print("I hope you find your way out.")
    print("Goodbye!")
    exit(0)
    
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
stuy91
  • 3
  • 2

1 Answers1

1

If you try this simple code

mylist = []
if "yes" or "y" in mylist:
    print("oops")

you'll see that the code is interpreted as

mylist = []
if "yes" or ("y" in mylist):
    print("oops")

And since

if "yes":
    print("oops")

it will always run through the if-part and never through the elif part. That's because "yes" is considered a truthy value.

What you probably wanted is

if take in ["y", "yes"]:

which is "check if the user input (take) is in the list ([]) of possible answers". And similar for the no-part of the statement.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222