![]() |
Homework advice - Boolean function, whole-word values - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Homework advice - Boolean function, whole-word values (/thread-26619.html) |
Homework advice - Boolean function, whole-word values - musicjoeyoung - May-07-2020 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: 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: 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") 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 RE: Homework advice - Boolean function, whole-word values - buran - May-07-2020 you want bird_types to be list /tuple /set - i.e. container type, not str
RE: Homework advice - Boolean function, whole-word values - Marbelous - May-07-2020 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. RE: Homework advice - Boolean function, whole-word values - buran - May-07-2020 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)}") RE: Homework advice - Boolean function, whole-word values - musicjoeyoung - May-07-2020 Thanks!!! |