Help please


def my_func(x):
my_var = 0
if x > my_var:
sum = my_var + x
print product
return sum

Oops, try again. Did you create a function called product? Your code threw a "global name 'product' is not defined" error.


Is this exercise 13?

Answer: No, you did not.

def product():

The instructions,

Define a function called product that takes a list of integers as input and returns the product of all of the elements in the list.

takes a list means there is one parameter variable, and the function expects it to be a list. The product of all the elements means we have to multiply them all together. Eg. a * b * c * ... We can use any list.

def product(x):
    # code
    return total

print product([2,5,7,9])   # 630

Given a list we would expect to iterate over the list. for is a useful construct to do this. We can set a starting value on total, 1 since unity when multiplied by anything does not change it so we aren't introducing any errors.

    # code
    total = 1
    for i in range(len(x)):
        total *= x[i]
    return total