Python Forum

Full Version: Homework advice - Boolean function, whole-word values
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Folks on here were so quick to help with my first question--I appreciate everyone who replies!

I completed a task assigned to me, but I'm missing some clarifying piece.
Quote:Instructions:
create this program with a Boolean function bird_available()
  • has parameter that takes the name of a type of bird
  • for this exercise the variable bird_types = 'crow robin parrot eagle sandpiper hawk pigeon'
  • return True or False (we are making a Boolean function)
  • call the function using the name of a bird type from user input
  • print a sentence that indicates the availability of the type of bird checked

Here's my code which make sense to me:
def bird_available(bird):
    bird_types = 'crow, robin, parrot, eagle, sandpiper, hawk, pigeon'
    return bird.lower() in bird_types
    


bird_input = bird_available(input("Type bird name here: "))

print("The bird is available T/F: ", bird_input)
Here's some normal, expected, and correct output examples:
Output:
Type bird name here: crow The bird is available T/F: True #or Type bird name here: dog The bird is available T/F: False
What happens, though, is that if I type in any consecutive letters from the values in bird_types, I get True. (e.g., "ro" from "crow", or "pig" from "pigeon")
Output:
Type bird name here: ro The bird is available T/F: True
Can someone help on how to address that aspect? I feel like the answer has something do to with the variable bird_types but I can't figure out what to do.
Any help is greatly appreciated!

-a brand new student to this
you want bird_types to be list/tuple/set - i.e. container type, not str
Your bird_types variable is a string so if the combo of letters is anywhere "in" it, you'll get a true.
I think you meant to make it a list with each word in quotes separated by commas. That will give yo the results you're expecting.
as a side note, it's much better to take bird as input and pass it to function, not do everything in one step. Note the use of f-string.
bird = input("Type bird name here: ")
print(f"The bird is available T/F: {bird_available(bird)}")
Thanks!!!