[Tutor] product of all arguments passed to function call

Cameron Simpson cs at cskk.id.au
Mon May 31 23:31:11 EDT 2021


On 31May2021 11:19, Alan Gauld <alan.gauld at yahoo.co.uk> wrote:
>You solution works for the case where at least 1 argument is passed.
>What if none are passed? A small change suffices:
>
>def product(*x):
>    result = 1 if x else 0
>    for n in x:
>      result *= n
>    return result
>
>If you don't like returning zero for an empty list you could
>raise an exception instead - ValueError perhaps?.

If he doesn't like zero (indeed, I don't either) he can use what he's 
got, because Python will raise a better error when the x parameter isn't 
supplied, with the bonus that linters may well catch it too.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list