Product stuck


#1



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


Your function fails on product([1, 2]). It returns 5 when it should return 2.


i don't know how to multiply 2 by 1 , instead of multiply 2 by itself and add to it result of 1 multiplyed by itself , sorry for my english


def product(numbers):
    lst = 0
    for i in numbers:
        if i != 0:
            i*=i
        lst+=i
    return lst


#2

First, you lst needs to be 1 when it's initialized. That way you can lst *= for each item in the given list. You don't need the if statement, because if there is a 0 in the list, then you want to multiply it even though it will end up being 0. You don't need to add i to lst.

def product(numbers):
    lst = 1
    for i in numbers:
        lst *= i
    return lst

#3

Thanks , man :slight_smile: It works


#4

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