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




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


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


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


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!


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