13:product pls help


#1

I don't get how to do this. I need help.

I don't know how to solve this. Can you please help?


def product(x):
    x = []
    for item in x:
        x *= 1
    return x


#2

You're pretty close.
If you do x*=1, you're basically just multiplying 1 to x for the number of items in x.
eg.
x = [1,2,3]
Your function will be multiplying 1 to a list 3 times. [] * 1 * 1 * 1 which doesn't make sense right? You can't multiply numbers to an empty list.
If you set x = 0 instead of x = [], your result is always going to be 0. because 0 * 1 * 1 * 1 = 0. So how about setting x = 1? That way, it can hold the numbers in the list x as it is.

Next,
your x*=1. It's just going to be multiplying by 1 and not the other numbers in the list, x (which is what the lesson wants). You already have your index set in your for loop. It basically visits all the items in the list each time.
So how about making x*= 1 to x*=item? This way, the function is multiplying all the numbers.
eg. x = [1,2,3], your function will calculate the list as this: 1(this 1 is from your total which you are accumulating)*1*2*3.


#3

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