Average calculation Program *need help*
Martin A. Brown
martin at linux-ip.net
Thu May 12 12:13:58 EDT 2016
Greetings kobsx4,
>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.
Suggestion #1:
--------------
When you are stuck on a small piece of code, set it aside (stop
looking at it) and start over again; sometimes rewriting with
different variable names and a clean slate helps to highlight the
problem.
Professional programmers will tell you that they are quite
accustomed to 'throwing away' code. Don't be afraid to do it.
(While you are still learning, you might want to keep the old chunk
of code around to examine so that you can maybe figure out what you
did wrong.)
Suggestion #2:
--------------
Put a print statement or two in the loop, so that you see how your
variables are changing. For example, just before your 'while' line,
maybe something like:
print "score=%d totalScores=%d" % (score, totalScores,)
Suggestion #3:
--------------
Break the problem down even smaller (Rustom Mody appears to have
beat me to the punch on that suggestion, so I'll just point to his
email.)
Hint #1:
--------
What is the value of your variable totalScores each time through the
loop? Does it ever get reset?
Good luck with your degubbing!
-Martin
--
Martin A. Brown
http://linux-ip.net/
More information about the Python-list
mailing list