Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.032 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'list...': 0.07; 'counting': 0.09; 'list).': 0.09; 'subject:How': 0.10; 'python': 0.11; 'def': 0.12; 'calculates': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'iterating': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'hey': 0.18; 'header:User-Agent:1': 0.23; 'header:In-Reply- To:1': 0.27; "doesn't": 0.30; 'list:': 0.30; 'easy,': 0.31; 'occurs': 0.31; '(i.e.': 0.33; 'subject:with': 0.35; 'but': 0.35; 'subject:?': 0.36; 'list': 0.37; 'work?': 0.38; 'to:addr:python- list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'length': 0.61; 'new': 0.61; 'numbers': 0.61; "you're": 0.61; 'times': 0.62; 'sum': 0.64; 'header:Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'william': 0.81; 'reply- to:addr:python.org': 0.84; 'divided': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=I5lcGrQg c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=0kkAYlmtguIA:10 a=Jww8p7Z7i0IA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=qWbBzqlYqigA:10 a=gSv7-QceeFgzpTrTlQEA:9 a=PRnIF-VoDmDWMNO8:21 a=_oK0vNHilB7zKlSl:21 a=wPNLvfGTeEIA:10 X-AUTH: mrabarnett:2500 Date: Tue, 17 Sep 2013 01:01:50 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 MIME-Version: 1.0 To: python-list@python.org Subject: Re: How do I calculate a mean with python? References: <571f1251-17a5-44db-a859-17a6d8065151@googlegroups.com> In-Reply-To: <571f1251-17a5-44db-a859-17a6d8065151@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1379376117 news.xs4all.nl 15873 [2001:888:2000:d::a6]:43268 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54256 On 17/09/2013 00:33, William Bryant 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 You're iterating through every number in the list... > for i in List: counting how many times each number occurs in the list: > thecount = List.count(i) This line calculates the sum of the numbers on every iteration: > thesum = sum(List) This line divides the sum of the numbers by the last count: > themean = thesum / thecount > > Why doesn't this work? > It does work; it just doesn't calculate the mean! What you end up with is: themean = sum(List) / List.count(40) What you _really_ want is the sum of the numbers divided by the number of numbers (i.e. the length of the list).