Quick question.....

Peter Abel PeterAbel at gmx.net
Fri Jan 16 07:42:47 EST 2004


featherstone80 at hotmail.com (Narsil) wrote in message news:<379178f1.0401151634.556bb405 at posting.google.com>...
> I tried that.  I get the following error message
> 
> Traceback (most recent call last):
>   File "E:\python\fun4b.py", line 80, in ?
>     menu()
>   File "E:\University\python\assign4b.py", line 49, in menu
>     print sum(weektotlist)
> NameError: global name 'sum' is not defined
> 
> What did I do wrong?
> 
> TomN
> 
> "Batista, Facundo" <FBatista at uniFON.com.ar> wrote in message news:<mailman.370.1074115102.12720.python-list at python.org>...
> > featherstone80 wrote:
> > 
> > #- numberlist = [10, 2, 5, 7, 3, 46, 4, 5, 87, 5]
> > #- 
> > #- How would I get the total, and how would I get the average?
> > 
> > 
> > Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
> > ...
> > >>> numberlist = [10, 2, 5, 7, 3, 46, 4, 5, 87, 5]
> > >>> sum(numberlist)
>  174
> > >>> len(numberlist)
>  10
> > >>> sum(numberlist)/len(numberlist)
>  17
> > >>> from __future__ import division
> > >>> sum(numberlist)/len(numberlist)
> > 17.399999999999999
> > 
> > .	Facundo

As David told you it depends on your Pyhton version, but
a sum-function is one-liner in Python:

>>> sum=lambda numberlist:reduce(lambda x,y:x+y,numberlist,0.0)
>>> # Remark, I use 0.0 as startvalue to force float
>>> numberlist = [10, 2, 5, 7, 3, 46, 4, 5, 87, 5]
>>> sum(numberlist)/len(numberlist)
17.399999999999999
>>> 

Regards
Peter



More information about the Python-list mailing list