13/15 Help! what am I doing wrong?


#1

that's my code, it keeps failing for the [1,2] case, i can't figure out what I am doing wrong.

def product(integers):
length = len(integers)
multiplier = 1

for i in range(length):
    multiplier = multiplier * integers[i]
    return multiplier

print multiplier
return multiplier

#2

on the last line, you return multiplier, do not understand. Why would you do that? I recommend you remove it.

print multiplier is to call your function i assume? but your function is called product, not multiplier. Plus you don't feed it any arguments (which i do recommend), so it should become something like:

print product([4,4])

that should work


#3

Hi, @darkhorse95 ,

The formatting at the beginning of your posted code somehow got lost, so it's not clear exactly how your lines are indented. The return line should only be indented by one level, so that it is part of the product function, but not part of the for loop.

Below is your code with correct indentation and two test cases. It works.

def product(integers):
    length = len(integers)
    multiplier = 1
    for i in range(length):
        multiplier = multiplier * integers[i]
    return multiplier

print product([1, 2])
print product([2, 3, 5, 7, 11])

Output:

2
2310

#4

Thank you for clarifying the print function


#5

Thanks, I indented correctly and it works now.


#6
def product (num_list):
    if len (num_list) == 0:
        return 0
    else:
        ans = 1
        for i in num_list:
            ans *= i
    return ans

print product([])
print product ([1, 2, 3, 4, 5, 5])