Product - stuck SOLVED


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/3?curriculum_id=4f89dab3d788890003000096#


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


#2

here:

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?


#3

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.


#4

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


#5

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!


#6

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