Product


#1



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


I get the following error message: "Oops, try again. Your function fails on product([4, 5, 5]). It returns 20 when it should return 100."


I don't quite know what i'm doing wrong.


def product(lst):
    lst_int = int()
    index = len(lst)
    for x in lst:
        index -= 1
        lst_int = x * lst[index]
    return lst_int
    
print product([1,2,3])
print 1 * 2 * 3


#2

You are overthinking this, try following this template:

def FUNCTION(PARAMETER):
    VARIABLE = 1
    for INDIVIDUAL_ITEMS in PARAMETER:
        VARIABLE *= INDIVIDUAL_ITEMS
    return VARIABLE

#3

Thank you very much! However, that very much resembles the original design of my function, with the only difference (as far as i remember) being that my VARIABLE = 0, which returned an error code as well.

If there any difference in having a variable = 0, and a variable = 1 in terms of the behaviour of the function?


#4

Yes, there is in fact a difference. Computers, unlike you and I, start counting at 0. So, in order for us to insure that a mathematical equation works, we must set our starting point at 1. This way the computer and program will know to start counting from 1.


#5

Okay. Thank you very much for your help!


#6

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