15 Exercises to Know A Programming Language

Martin martin.clausen at gmail.com
Tue Jul 3 06:47:42 EDT 2007


On Jul 3, 12:25 pm, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> On Tue, 03 Jul 2007 09:58:16 +0000, Martin wrote:
> > "Write a program that takes as its first argument one of the words
> > 'sum,' 'product,' 'mean,' or 'sqrt'  and for further arguments a
> > series of numbers. The program applies the appropriate function to
> > the series."
>
> > My solution so far is this:
>
> >http://dpaste.com/13469/
>
> > I would really like some feedback. Is this a good solution? is it
> > efficient? robust? what could be improved? any not looking for a
> > revised solution, hints on what to improve are also very welcome.
>
> Don't use `eval()` if it is not absolutely necessary.  Especially if the
> input comes from a user it's a security hole.  `float()` is the function
> to use here.
>
> `mean()` does not work as you try to divide a list by a number.
>
> Ciao,
>         Marc 'BlackJack' Rintsch

Thanks for the feedback. I have posted a revised version here (http://
dpaste.com/13474/) where mean works. The reason I use eval is I want
it to work for complex numbers too, but I guess i could check for the
presence of a "j" in the arguments instead.

Martin




More information about the Python-list mailing list