Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: MRAB Newsgroups: comp.lang.python Subject: Re: Average calculation Program *need help* Date: Thu, 12 May 2016 17:14:52 +0100 Lines: 34 Message-ID: References: <011a2f60-aafd-484f-8cb2-6017aaf1bb29@googlegroups.com> <5c7d5f21-62ad-bb66-fbd2-4f4c50069881@mrabarnett.plus.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de UGaK5rVK0fjeIcDmFoItsADLtP45yZhHspqNtlS6siUA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.041 X-Spam-Evidence: '*H*': 0.92; '*S*': 0.00; 'subject:help': 0.07; 'def': 0.13; '"your': 0.16; '#this': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'range(0,': 0.16; 'range,': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'basically': 0.18; 'subject:need': 0.18; 'all,': 0.20; 'are.': 0.22; 'ok.': 0.22; 'help.': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; 'error': 0.27; 'function': 0.28; 'checking.': 0.29; 'code:': 0.29; "i'm": 0.30; 'print': 0.30; '(including': 0.30; 'code': 0.30; 'checked': 0.31; 'supposed': 0.31; 'received:84': 0.32; 'run': 0.33; 'problem': 0.33; 'add': 0.34; 'something': 0.35; 'asking': 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'doing': 0.38; 'someone': 0.38; 'test': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'email addr:gmail.com': 0.62; 'total': 0.62; 'times': 0.63; 'hours': 0.65; 'between': 0.65; 'repeat': 0.67; 'score': 0.76; 'number):': 0.84; 'score:': 0.84; 'subject: *': 0.84; 'want:': 0.84; 'scores': 0.91; 'average': 0.93; 'subject:*': 0.93 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=bsGxfxui c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=IkcTkHD0fZMA:10 a=pGLkceISAAAA:8 a=2f_g7eNmzRDVwgYhCuIA:9 a=U9GkvII9oZAMluHC:21 a=AatsLAx4q_SfIV6o:21 a=QEXdDO2ut3YA:10 a=6kGIvZw6iX1k4Y-7sg4_:22 X-AUTH: mrabarnett@:2500 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.0 In-Reply-To: <011a2f60-aafd-484f-8cb2-6017aaf1bb29@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <5c7d5f21-62ad-bb66-fbd2-4f4c50069881@mrabarnett.plus.com> X-Mailman-Original-References: <011a2f60-aafd-484f-8cb2-6017aaf1bb29@googlegroups.com> Xref: csiph.com comp.lang.python:108564 On 2016-05-12 16:47, kobsx4@gmail.com wrote: > Hello all, I have been struggling with this code for 3 hours now and I'm still stumped. My problem is that when I run the following code: > ------------------------------------------------------------------------------ > #this function will get the total scores > def getScores(totalScores, number): > for counter in range(0, number): > score = input('Enter their score: ') > totalScores = totalScores + score > > while not (score >= 0 and score <= 100): > > print "Your score must be between 0 and 100." > score = input('Enter their score: ') > > > > return totalScores > ------------------------------------------------------------------------------ > the program is supposed to find the average of two test scores and if one of the scores is out of the score range (0-100), an error message is displayed. The main problem with this is that when someone types in a number outside of the range, it'll ask them to enter two scores again, but ends up adding all of the scores together (including the invalid ones) and dividing by how many there are. Please help. > Basically what you want is: repeat as many times as there are scores you want: ask for the score while it's invalid: ask for it again add it to the total What you're actually doing is asking for the scores and adding them _before_ checking. Don't include something before you've checked that it's OK.