Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #54259
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <cameron@cskk.homeip.net> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'output': 0.05; 'odd': 0.07; 'variables': 0.07; 'mail:': 0.09; 'parameter': 0.09; 'pretend': 0.09; 'teh': 0.09; 'subject:How': 0.10; 'python': 0.11; 'def': 0.12; 'calculates': 0.16; 'doing,': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'incorrect': 0.16; 'iteration': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'received:211.29': 0.16; 'received:211.29.132': 0.16; 'received:cskk.homeip.net': 0.16; 'received:homeip.net': 0.16; 'received:optusnet.com.au': 0.16; 'received:syd.optusnet.com.au': 0.16; 'rfc-822': 0.16; 'simpson': 0.16; 'variables;': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'hey': 0.18; 'variable': 0.18; 'bit': 0.19; '(but': 0.19; 'print': 0.22; 'header:User-Agent:1': 0.23; 'tend': 0.24; 'cheers,': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; "doesn't": 0.30; 'list:': 0.30; 'program,': 0.31; 'easy,': 0.31; 'figure': 0.32; 'run': 0.32; 'subject:with': 0.35; 'classes': 0.35; 'definition': 0.35; 'but': 0.35; 'there': 0.35; 'received:com.au': 0.36; 'doing': 0.36; 'charset:us-ascii': 0.36; 'subject:?': 0.36; 'should': 0.36; 'wrong': 0.37; 'list': 0.37; 'received:211': 0.38; 'work?': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'lower': 0.61; 'new': 0.61; 'times': 0.62; 'content-disposition:inline': 0.62; 'name': 0.63; 'more': 0.64; 'therefore': 0.72; 'upper': 0.74; 'william': 0.81; 'does?': 0.84; 'mean.': 0.91; 'why?': 0.91 |
| Date | Tue, 17 Sep 2013 09:59:45 +1000 |
| From | Cameron Simpson <cs@zip.com.au> |
| To | python-list@python.org |
| Subject | Re: How do I calculate a mean with python? |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Content-Disposition | inline |
| In-Reply-To | <571f1251-17a5-44db-a859-17a6d8065151@googlegroups.com> |
| User-Agent | Mutt/1.5.21 (2010-09-15) |
| References | <571f1251-17a5-44db-a859-17a6d8065151@googlegroups.com> |
| X-Optus-CM-Score | 0 |
| X-Optus-CM-Analysis | v=2.1 cv=bpB1Wiqi c=1 sm=1 tr=0 a=YuQlxtEQCowy2cfE5kc7TA==:117 a=YuQlxtEQCowy2cfE5kc7TA==:17 a=ZtCCktOnAAAA:8 a=PO7r1zJSAAAA:8 a=LcaDllckn3IA:10 a=Jww8p7Z7i0IA:10 a=kj9zAlcOel0A:10 a=vrnE16BAAAAA:8 a=8AHkEIZyAAAA:8 a=qWbBzqlYqigA:10 a=pGLkceISAAAA:8 a=pNGmx8qeAAAA:8 a=w1VTR14e2Vx5La_6Gp4A:9 a=aIFMf329XZmIK2S8:21 a=Zu1q-jiGs50YH5sj:21 a=CjuIK1q_8ugA:10 a=MSl-tDqOz04A:10 a=MY0F4kz7x_AA:10 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.44.1379377672.18130.python-list@python.org> (permalink) |
| Lines | 52 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1379377672 news.xs4all.nl 15938 [2001:888:2000:d::a6]:54912 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:54259 |
Show key headers only | View raw
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