Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #54259
| Date | 2013-09-17 09:59 +1000 |
|---|---|
| From | Cameron Simpson <cs@zip.com.au> |
| Subject | Re: How do I calculate a mean with python? |
| References | <571f1251-17a5-44db-a859-17a6d8065151@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.44.1379377672.18130.python-list@python.org> (permalink) |
On 16Sep2013 16:33, William Bryant <gogobebe2@gmail.com> wrote:
| Hey I am new to python so go easy, but I wanted to know how to make a program that calculates the maen.
|
| List = [15, 6, 6, 7, 8, 9, 40]
| def mean():
| global themean, thesum
| for i in List:
| thecount = List.count(i)
| thesum = sum(List)
| themean = thesum / thecount
|
| Why doesn't this work?
Well:
- always include the output you get from your program, and say
why you think it is incorrect
- just style: we tend to name variables in lower case in Python, and
classes with an upper case letter; "List" is a bit odd (but
"list" is taken; how about "values"?)
- more than style: WHY are you using global variables; just return the mean
from the function!
- teh variable "List" inside the function is _local_; your function does not
accept a parameter
- sum(List) sums the whole list
you run it many times
why?
- what do you think "count()" does?
- you should print i, thecount and thesum on each iteration of
the list; it will help you see what your function is doing, and
therefore to figure out what it is doing wrong
I would write a mean like this:
def mean(values):
return sum(values) / len(values)
There are circumstances where that is simplistic, but it is the classic
definition of the mean.
Cheers,
--
Cameron Simpson <cs@zip.com.au>
Microsoft Mail: as far from RFC-822 as you can get and still pretend to care.
- Abby Franquemont-Guillory <abbyfg@tezcat.com>
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How do I calculate a mean with python? William Bryant <gogobebe2@gmail.com> - 2013-09-16 16:33 -0700
Re: How do I calculate a mean with python? Jugurtha Hadjar <jugurtha.hadjar@gmail.com> - 2013-09-17 00:52 +0100
Re: How do I calculate a mean with python? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-16 23:55 +0000
Re: How do I calculate a mean with python? MRAB <python@mrabarnett.plus.com> - 2013-09-17 01:01 +0100
Re: How do I calculate a mean with python? Jugurtha Hadjar <jugurtha.hadjar@gmail.com> - 2013-09-17 01:08 +0100
Re: How do I calculate a mean with python? Cameron Simpson <cs@zip.com.au> - 2013-09-17 09:59 +1000
Re: How do I calculate a mean with python? Dave Angel <davea@davea.name> - 2013-09-17 00:50 +0000
Re: How do I calculate a mean with python? Travis Griggs <travisgriggs@gmail.com> - 2013-09-17 08:25 -0700
Re: How do I calculate a mean with python? William Bryant <gogobebe2@gmail.com> - 2013-09-17 12:44 -0700
Re: How do I calculate a mean with python? William Bryant <gogobebe2@gmail.com> - 2013-09-17 12:45 -0700
Re: How do I calculate a mean with python? William Bryant <gogobebe2@gmail.com> - 2013-09-17 13:10 -0700
Re: How do I calculate a mean with python? Joel Goldstick <joel.goldstick@gmail.com> - 2013-09-17 17:02 -0400
Re: How do I calculate a mean with python? MRAB <python@mrabarnett.plus.com> - 2013-09-17 22:26 +0100
Re: How do I calculate a mean with python? William Bryant <gogobebe2@gmail.com> - 2013-09-17 14:31 -0700
Re: How do I calculate a mean with python? Joel Goldstick <joel.goldstick@gmail.com> - 2013-09-17 19:49 -0400
Re: How do I calculate a mean with python? Bryan Britten <britten.bryan@gmail.com> - 2013-09-17 18:53 -0700
csiph-web