Product


#1



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


Oops, try again. Your function crashed on [4, 5, 5] as input because your function throws a "list index out of range" error.


the result is correct but sthg wrong


def product(l):
    p = 1
    for a in range(l[0],l[len(l)-1]):
        p *= l[a]
    return p
print product([1,2,3,4,5])    
b = 1*2*3*4*5
print b


#2

here:

l[len(l)-1]

i get length minus one to calculate till where the loop should loop, but no need to retrieve the value from list using square brackets


#3

sorry but I don't understand what u want say?


#4

why l[]? length minus one is a perfect stop value for range


#5

You don't need index looping here.
You just can loop over it.

We learnt two types of looping ...

Don't use index looping over a sequence

tab = [1,2,3,4]

for i in range(len(tab)) :
    print tab[i]

rather use this one..

for j in tab :
    print j

You'll get rid of many complexities of code that you have now!
I hope it helps you!


#6

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