Product


#1



Hey guys, I would like you to tell me why my code doesn't work, it shows the following error:
Oops, try again. Your function crashed on [1, 2] as input because your function throws a "list index out of range" error.


def product(lst):
    for i in lst:
        result = lst[i]
        if lst[i] != lst[len(lst) - 1]:
            lst[i] = lst[i] * lst[i + 1]
            
        elif lst[i] == len(lst):
            pass
    return result


#2

Doesn't that error message already explain that? Are you wondering something else?
You've answered your own question so if you mean to ask something else, then that's what you'll need to do - ask about the thing you want to know.


#3

You made it too complicated - all you need is to set the variable "total" to 1 and then multiply it by all the elements of your list:

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

#4

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