[Tutor] the products of an element of a list

Tahir Hafiz tahir.hafiz at gmail.com
Sat Dec 26 10:10:35 EST 2015


Thanks Steven and Peter, I de-dented and changed the variable name and it
works now.
Cheers,
Tahir

On Sat, Dec 26, 2015 at 1:42 PM, Peter Otten <__peter__ at web.de> wrote:

> Tahir Hafiz wrote:
>
> > 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.
>
> Look at the indentation. When does execution of the function end? Where
> should it?
>
> If you don't see it yet use another example list, one that doesn't start
> with 1.
>
> Example: what does
>
> >>> prod([2, 3, 4])
>
> return? Is the for loop entered? If yes, for how many iterations?
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list