-1

I am starting with python and facing a basic issue with this nesting exercise:

bird_names="eagle", "pigeon", "colibri", "seagull"

bird_guess=input("Guess the bird: ")

if bird_guess in bird_names == False:
    print("Try again")
    bird_guess2=input("Guess the Bird: ")   

    if bird_guess2 in birds == False:
        print("Try again")
        bird_guess3=input("Guess the Bird: ") 

        if bird_guess3 in birds == False:
            print("Sorry out of tries")     
        else:
            print("Yes 3rd try")
    else:
        print("Yes 2rd try")
else:
    print("Great, 1st try")

When I run it and assing to bird_guess the value d for example, the code outputs direct Great, 1st try instead of going through all the other if statemnts.

Any idea where is my error?

ruohola
  • 21,987
  • 6
  • 62
  • 97
GCGM
  • 901
  • 1
  • 17
  • 38

3 Answers3

4

This:

if bird_guess in bird_names == False:

is the same as:

if bird_guess in bird_names and bird_names == False:

You should do this instead:

if bird_guess not in bird_names:

to check if an item is not in a list.

 

The reason why your original expression didn't work is comparison chaining. In Python arbitrary amount of comparisons can be chained together, so for example x < y <= z is equivalent to x < y and y <= z. And thus a in b == False will become a in b and b == False.

ruohola
  • 21,987
  • 6
  • 62
  • 97
3

Starting out with a new language is great! Besides learning the language details, become also aware on how to write extendable code.

What happens, if you'd like to gave the user 5 instead of 3 guesses? You'll end up with 5 levels of ifs. An alternative to the nested ifs is a loop:

max_tries = 3
bird_names = ["eagle", "pigeon", "colibri", "seagull"]

for current_try in range(max_tries):
    guess = input("Guess the bird: ")
    if guess in bird_names:
        print(f"Yes, {current_try+1}. try!")
        break
    elif current_try < max_tries-1:
        print("Try again")
    else:
        print("Sorry, out of tries")
paulw
  • 31
  • 1
-1
# [ ] Create the "Guess the bird" program 

# [ ] nested conditionals
# if
   # if
       # if
       # else
    #else
#else



bird_guess = input()
bird_name = "eagle, falcon, crow, dove, pigeon," 

bird_guess in bird_name

if bird_guess == "eagle":
    print("1st Try!: ", bird_guess in bird_name)
    bird_guess = input() # added variable to continue the code

if bird_guess =="crow":
    print("2nd Try!: ", bird_guess in bird_name)
    bird_guess = input()
# [ ] added if conditions is to continue the bird_guess game option 
if bird_guess =="falcon":
    print("3rd Try!: ", bird_guess in bird_name)
    bird_guess = input()

if bird_guess =="dove":
    print("4th Try!: ", bird_guess in bird_name)
    bird_guess = input()

if bird_guess =="pigeon":
    print("5th Try!: ", bird_guess in bird_name)
    bird_guess = input()

# [ ]  the added else & if is to continue code if the nested if answer is incorrect 
else:
    print("Try Again!: ")
    bird_guess = input()
if bird_guess =="crow":
    print("2nd Try!: ", bird_guess in bird_name)
    bird_guess = input()



else:
    print("2 Try Again!: ")
    bird_guess = input()
if bird_guess =="falcon":
    print("3nd Try!: ", bird_guess in bird_name)
    print("Sorry Out turn: ") # [ ]last else & if will not need variable  given there won't be any option or guess after words
else:
    print("game over")
Savant
  • 1
  • 1
  • 1
    Welcome to Stack Overflow! Thank you for this code snippet, which might provide some limited short-term help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its long-term value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Toby Speight Feb 12 '20 at 15:24
  • you should [edit] the answer to include the explanation, rather than writing it in a comment! – Toby Speight Feb 13 '20 at 16:25