[Tutor] the products of an element of a list

Tahir Hafiz tahir.hafiz at gmail.com
Sat Dec 26 07:59:45 EST 2015


Finally getting round to doing some more python.
Stuck on a little online exercise:
"Define a function prod(L) which returns the product of the elements in a
list L."

Let's say L = [1, 2, 3, 4]

I have done this so far but it's not quite working:



def prod(L):

   sum = 1
   for i in L:


      sum = sum * i


      return sum


Code seems to print the right value in REPL (i.e. 24) just a for loop
though !?!
Think I am not seeing something obvious.


Cheers,
Tahir


More information about the Tutor mailing list