Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python function that iterates a menu of three lists of strings issues

I have to write a function that takes three dishes and an ingredient and outputs true if the ingredient isn’t in the dish and false if any of the dishes contain the ingredient.

I’ve been trying this for hours and initially was having an issue where it was either outputting true or false for all test cases regardless. I have now tried using any() but its returning a TypeError: ‘bool’ object is not iterable and I’m getting really confused and frustrated.

any advice would be much appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def free_from(menu: list, ingredient: str):
    """Return whether ingredient is in dish.
    
    Preconditions: menu = dish1, dish2, dish3
    Postconditions: if menu has ingredient = false else true
    """
    for dish in menu:
        if any(ingredient in menu):
            return False
    return True

menu = [
         ['soup','onion','potato','leek','celery'],
         ['pizza','bread','tomato','cheese','cheese'],
         ['banana']
       ]

>Solution :

for dish in menu:
    if ingredient in dish:
        return False
return True
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading