Average calculation Program *need help*

MRAB python at mrabarnett.plus.com
Thu May 12 12:14:52 EDT 2016


On 2016-05-12 16:47, kobsx4 at 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.




More information about the Python-list mailing list