Average calculation Program *need help*

Nick Sarbicki nick.a.sarbicki at gmail.com
Fri May 13 03:10:17 EDT 2016


On Fri, 13 May 2016, 06:52 Jake Kobs, <kobsx4 at gmail.com> wrote:

> Thank you for the help..I think I'm getting closer, but I feel like after
> they enter an invalid number, it should reset the invalid number(s)
> somehow. Here's my updated code:
>
> ------------------------------------------------------------------------------
> #this function will get the total scores
> def getScores(totalScores, number):
>     for counter in range(0, number):
>         score = input('Enter their score: ')
>         if (score < 100 and score > 0):
>             totalScores = totalScores + score
>     while (score > 100 or score < 0):
>
>         print "Your scores must be between 0 and 100."
>         score = input('Enter their score: ')
>         score = input('Enter their score: ')
>         totalScores = totalScores + score
>
>
>     return totalScores
>
> ----------------------------------------------------------------------------
> --
>

You're starting to get it which is good.

The if is a good idea to begin with, but your while loop still executes
after the for loop finishes as it is not properly indented.

As Michael said, indentation indicates what statement has ownership of a
block. Currently your while loop has it's own block outside the for loop.

This means it gets executed after your for loop has finished looping
through your range. You want the while to be held within the for loop so it
gets executed immediately after each input statement.

Think. Your if is inside the for loop and gets executed after every input.
Your input is inside the for loop. Your while and it's contents are not in
the for loop and get executed after the for loop finishes. The only
difference that matters here is indentation.

Try solve that bit first.

>



More information about the Python-list mailing list