I am properly stuck on 13/15 - any hints regarding a working code would be much appreciated!
def product(p): q = 1 for i in range(len(p)): q = q * p[i] return q print(product([4, 5, 5]))
def product("""list of numbers as in example"""): """counter""" = 1 #because multiplying by 0 is 0 for """each element in your list""" in """your list""": # your counter should be multiplied by each element in your list \n and then return the result of this multiplying
Hope this helps.
Here is my simple code
def product(numbers): t=1 for i in numbers: t=t*i return t
Great hint. thanks for not spelling it out directly!
def product(numbers): total = 1 # Total is 1 because anything times zero equals zero total = int(total) # total has to be an integer for x in numbers: total *= x # Here, this means that it multiplies x by the total, and does so for each return total
Yours works well. I took a different approach on the for loop and did this:
def product(list): pro = 1 for number in list: pro *= number return pro
Thanks for the comment
Quite elegant solution!
I found and learned what the "reduce" function does and came up with this:
def product(int_lst): return reduce(lambda x,y: x * y, int_lst)
Here is my code, it's very simple to understand:
for i in lst:
result *= i