Function depends on other function how we call the the function

def is_all_berries(fruits):
    """Return True if all items in fruits are valid berries."""

    for fruit in fruits:
        if not is_berry(fruit):
            return False

    return True

is_all_berries(["strawberry", "raspberry", "blackberry", "currant"]))

def is_berry(fruit):
    """Return True if fruit is a valid berry.

    Valid berries are "strawberry", "raspberry", "blackberry", and "currant".
    There are no other valid berries. For example, "berry" is not a valid berry.
is_all_berries(["apple", "berry", "cherry"]))


check_valid_fruit = lambda fruit: any(fruit == valid_fruit for valid_fruit \
in ['strawberry', 'raspberry', 'blackberry', 'currant'])


check_all_berries = lambda fruits: all(check_valid_fruit(fruit) \
for fruit in fruits)
check_all_berries(['strawberry', 'raspberry'])

check_all_berries(['apple', 'berry', 'cherry'])


One thing I’ve not seen pointed out: you’re calling is_all_berries()
before you define the is_berry() function.

When you run the above code, Python performs things as they are seen.

Put all the function definitions at the top.

All the calling code needs to come after the functions are defined.

Cameron Simpson

Yes that’s true it always gives me error is_berry not define but the problem is how to code define the function or call the function as this is my first program to call the function so little confused

Fundamentally it is no different to this:

 x = 1

which will complain that x is not defined, versus:

 x = 1

which will work, because we assign to x before we try to print it (or
use it in any other way).

A function is a named this just like x above, so this:


 def f(a):
     return a * 2

will also complain that f is not yet defined, while this:

 def f(a):
     return a * 2


will work, because we define f before we try to call it.

For this reason, it is normal to define all your functions before you
call them.

Cameron Simpson

how i define is_berry function gives an error

Your docstring specifies it well.
You can implement those specifications by testing fruit against each of the valid berries. == and or operators are useful there.
Or, if you’re familiar with sets and the in operator, you can use those for a less repetitious version.