Python Forum
Homework advice - Boolean function, whole-word values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Homework advice - Boolean function, whole-word values
#1
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
Reply
#2
you want bird_types to be list/tuple/set - i.e. container type, not str
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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.
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply
#4
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)}")
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thanks!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Basic Boolean homework help StillAnotherDave 9 3,576 Nov-27-2019, 12:38 PM
Last Post: StillAnotherDave
Exclamation A function that takes a word and a string of forbidden letters pooyan89 5 4,673 Jun-12-2019, 09:44 PM
Last Post: nilamo
  Storing Minimum List of values from a recursive function sigsegv22 1 2,544 Sep-10-2018, 01:25 PM
Last Post: ichabod801
  Help with homework problem - iterating a function midnitetots12 4 3,497 Feb-21-2018, 10:51 PM
Last Post: nilamo
  define all function, interogate list, boolean blacksaber 3 3,480 Jan-17-2017, 12:51 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020