2

I am running this code:

def guess_bird():
    birds = "Parrot, Owl, Penguin, Emu"
    bird_guess = input("Guess a bird. ")
    print(bird_guess in birds)
    if bird_guess in birds == True:
        print("Correct! First try. ")
    else:
        print("incorrect. Try again. ")
        bird_guess = input("Guess a bird. ")
        if bird_guess in birds == True:
            print("Correct! Second try. ")
        else:
            print("incorrect. Try again. ")
            bird_guess = input("Guess a bird. ")
            if bird_guess in birds == True:
                print("Correct! Third try. ")
            else:
                print("Incorrect. that was the last try you get! ")
guess_bird()

and when running this the output is

Guess a bird. Emu     
True    
Incorrect. Try again.   
Guess a bird. _

I expected it to run as saying Emu is in birds and the if statement exiting. I do not know why this is running like so, please help

Image of my code where I wrote it. I also didn't import any libraries.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 2
    dont check `== True`, just checking membership is already returning a boolean True/False. – Paritosh Singh Mar 16 '19 at 21:01
  • Also sidenote please use a while loop or something to keep asking for an input i am getting a slight discomfort reading the current state of continuous else statements – Moller Rodrigues Mar 16 '19 at 21:03
  • If you have a solution please post it as an answer instead of editing it into the question. This way if it is good people can vote it up and you'll get credit for it. As it stands now this question appears unanswered because there are no posted answers. – Jason Aller Mar 17 '19 at 03:28
  • Compare `(1 in [1,2]) == True` with `1 in ([1,2] == True)`. It's because `==` binds stronger than `in`. – lambda.xy.x Dec 30 '21 at 12:53

0 Answers0