3 Replies - 760 Views - Last Post: 12 May 2016 - 10:01 AM Rate Topic: -----

#1 kobs20   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 07-May 16

Determining Average Program help

Posted 11 May 2016 - 07:32 PM

This isn't necessarily an error, but incorrect formatting or something:
#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 score > 100 or score < 0:
        
        print "Your scores must be between 0 and 100."
        score = input('Enter their score: ')
        
    return totalScores


This is the output once I type in 2 invalid numbers followed by valid ones.
How many students took the test: 2
Enter their score: 555
Enter their score: 444
Your scores must be between 0 and 100.
Enter their score: 34
The average test score is 499
Do you want to end program? (Enter no to process a new set of scores):  

As you can see, it wont even let me type in 2 more scores after the first two invalid ones. Plus, it averages the three numbers.. Please help

Is This A Good Question/Topic? 0
  • +

Replies To: Determining Average Program help

#2 kobs20   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 07-May 16

Re: Determining Average Program help

Posted 11 May 2016 - 08:11 PM

Please help, I have no idea what the faulty code could be..
Was This Post Helpful? 0
  • +
  • -

#3 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 1025
  • View blog
  • Posts: 3,942
  • Joined: 13-June 14

Re: Determining Average Program help

Posted 11 May 2016 - 09:07 PM

Don't you want to validate each score? Given that information, where do you think that while loop should be?

A couple of other things:

1. Why are you both passing totalScores and returning it? You should either calculate the total locally and return it, or pass the variable in which to hold the total and modify it. There's no need to do both.

2. You don't need to pass the 0 to range(), since per the documentation, the start argument defaults to 0.
Was This Post Helpful? 0
  • +
  • -

#4 CurlyJoe   User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 97
  • Joined: 19-September 13

Re: Determining Average Program help

Posted 12 May 2016 - 10:01 AM

Quote

1. Why are you both passing totalScores and returning it?
Each function call increases the value in totalScores, so the function has to know what the score is now, and also has to return it after adding the next score.

Quote

As you can see, it wont even let me type in 2 more scores after the first two invalid ones.
Good job of testing to catch this. The while() is on the same indentation level as the for(), so gets an entry after the for() has executed.

def getScores(totalScores, number):
    for counter in range(0, number):
        score = input('Enter their score: ')
        while score > 100 or score < 0:
        
            print "Your scores must be between 0 and 100."
            score = input('Enter their score: ')
        ## only add after a "good" score was input
        totalScores = totalScores + score
        
    return totalScore 
Finally, if you are using Python3.X, input returns a string, so you will have to convert to an int.

This post has been edited by CurlyJoe: 12 May 2016 - 10:09 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1