Product - stuck SOLVED


Oops, try again. Your function fails on product([4, 5, 5]). It returns 5 when it should return 100.

I think the code just prints out number 1 of the list of numbers instead of multiplying it by total. No clue how to do this.

def product(x):
    total = x[1]
    for n in x:
        n *= n * len(x)
    return total



n *= n * len(x)

you do understand the *= shorthand? this means you wrote:

n = n * n * len(x)

also, what are you doing with length? the product of [4,5,4] is 80 (4 * 5 * 4) i don't see how list
length is relevant is here?


If you do this, you are going to multiply the second item of your list twice since your loop will go through every item of the list.


Thansk, seems like I overcomplicated things when the solution was quite simple. Obviously I was confused about the "*=".


Got, it, changed the code to:
def product(x):
total = 1
for n in x:
total *= n
return total
now works all right. Thanks for your help!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.