Click here to Skip to main content
15,796,738 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to take the user inputs of bird names and check in the list of bird_names.

I have tried the following code:
first input: butterfly
output:
2nd try fail, please do 3rd try

On 2nd try
input: parrot
output: <
1st try fail, do 2nd try
'


The code in italics is not working.

Kindly tell me where I am wrong in my logic.

Thanks in advance!

What I have tried:

bird_names=('crow', 'parrot', 'eagle')
def bird_guess():
    bird_guess1=input('Enter the bird guess :')
    return bird_guess1

bird_guess()

if(bird_guess not in bird_names):
        print('1st try fail, do 2nd try')
         bird_guess()
        if(bird_guess not in bird_names):
            print('2nd try fail, please do 3rd try')
            bird_guess()
            if(bird_guess not in bird_names):
                print('Sorry, exhausted tries')
            else:
                print('corect on 3rd try')
        elif(bird_guess  in bird_names):
                    print('good work! correct on 2nd try')
elif(bird_guess  in bird_names): 
                        print('great work!, correct on 1st try itself')
    
else:
    print('Not Valid')
Posted
Updated 12-Feb-20 4:46am
v2
Comments
Richard MacCutchan 7-Jun-18 5:22am    
Why not just use a simple loop that counts the number of tries, and exit the loop when maximum try count is reached?

Also, you are not capturing the value returned by your function.
Member 13842073 7-Jun-18 5:37am    
Hi Richard,
Thanks for the reply.

I have tried capturing the value and still getting the same result.

I am new to programming, and lagging in certain concepts.

Could you please further explain on this?
Thanks again
Member 13842073 7-Jun-18 6:24am    
Why is it mandated to capture the return value of a function?
Richard MacCutchan 7-Jun-18 6:31am    
Because you need to capture the return value in a variable. If you do not do that then the value is thrown away. Think of it as asking someone to write something on a piece of paper for you. When they have finished, you need to take the paper in order to see what they have written.
Member 13842073 7-Jun-18 6:46am    
Cool! Thank you so much for explaining

You are using bird_guess function, instead of its return value in your tests. Try
Python
bird_names=('crow', 'parrot', 'eagle')
def bird_guess():
    bird_guess1=input('Enter the bird guess :')
    return bird_guess1

guess = bird_guess()

if(guess not in bird_names):
    print('1st try fail, do 2nd try')
    guess = bird_guess()
    if(guess not in bird_names):
        print('2nd try fail, please do 3rd try')
        guess = bird_guess()
        if(guess not in bird_names):
            print('Sorry, exhausted tries')
        else:
           print('corect on 3rd try')
    else:
       print('good work! correct on 2nd try')
else:
    print('great work!, correct on 1st try itself')
 
Share this answer
 
Comments
Member 13842073 7-Jun-18 6:15am    
Bingo!
When I tried capturing in a variable, I did it once, somewhat like this:
x= bird_guess()
if(x not in bird_names):
print('1st try fail, do 2nd try')
bird_guess()
if(x not in bird_names):
print('2nd try fail, please do 3rd try')
bird_guess()

Got my mistake.
Thanks a lot!!
CPallini 7-Jun-18 6:38am    
You are welcome.
Try this:
Python
bird_names=('crow', 'parrot', 'eagle')
def bird_guess():
    bird_guess1=input('Enter the bird guess :')
    return bird_guess1

def birdtest():

    for count in range(1,4):
        bird = bird_guess()
        if(bird not in bird_names):
            print('try', count, 'failed, ', end='')
            if count < 3:
                print('try again')
            else:
                print('no more guesses allowed.')
                break
        else:
            print("correct, it is in the list")
            break

But, as a new programmer your time would be better spent following The Python Tutorial[^]
 
Share this answer
 
Comments
Member 13842073 7-Jun-18 6:17am    
Thanks, Richard.
Once I am through the basics, I will try your solution as well.
Thanks again.
# [ ] Solution

# [ ] Create the "Guess the bird" program




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()

if bird_guess =="crow":
print("2nd Try!: ", bird_guess in bird_name)
bird_guess = input()

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()


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: ")
 
Share this answer
 
v2
Comments
CHill60 12-Feb-20 9:53am    
Don't do it this way. Really, don't do it this way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900