OBJECTIVES: DETAILS: The average is the sum of all values divided by the number
ID: 3614765 • Letter: O
Question
OBJECTIVES:
DETAILS:
The average is the sum of all values divided by the number ofvalues. The median is the middle score when the scores are sorted,if there is an odd number of scores. If there is an even number ofscores it is the average of the two middle scores. The mode is thescore that occurs most frequently. If there are two or more scoresthat occur most frequently, then the mode is undefined. If youdon't do the extra credit you may assume there will be one mostfrequently occuring score. See the extra credit below for anaddition.
INPUTS:
The scores, one per line, with the last value being -1.
OUTPUTS:
The scores in descending order:
100
90
80
70
70
The average is: 82
The high score is: 100
The low score is: 70
The median is: 80
The mode is: 70
DETAILS:
Use good programming style for your code. Here are some things youshould include to have good programming style in your code
Explanation / Answer
stjohn@beatific ~/programming/cramster $ python a.py This program does statistics on a series of entered values. Score 1 (-1 ends): 70 Score 2 (-1 ends): 90 Score 3 (-1 ends): 80 Score 4 (-1 ends): 100 Score 5 (-1 ends): 70 Score 6 (-1 ends): -1 The scores in descending order: 100 90 80 70 70 The average is 82 The high score is 100 The low score is 70 The median is 80 The mode is 70 stjohn@beatific ~/programming/cramster $ Here is the code: #!/usr/bin/python def average(scores): return`sum(scores)/len(scores)` def sortHighToLow(scores): scores.sort(reverse=True) return scores def printScores(scores): for n in scores: print n def high(scores): return `scores[0]` def low(scores): return `scores[len(scores)-1 ]` def median(scores): remainder =len(scores)%2 middle =len(scores)%2 if remainder == 0: return `(scores[middle] + scores[middle+1]) /2` else: return `scores[middle+1]` def mode(scores): freq = [ ( n,scores.count(n) ) for n in set(scores) ] freqSorted =sorted(freq, key=lambda x: -x[1]) if freqSorted[0][1] ==freqSorted[1][1]: return "not defined" else: return `freqSorted[0][0]` def main(): scores = [] ends = False counter = 0 print "This program doesstatistics on a series of entered values. " while not ends: counter += 1 input = raw_input("Score %s (-1 ends): " % counter) if input == '-1': ends = True else: scores.append( int(input) ) scores =sortHighToLow(scores) print "The scores indescending order: " printScores(scores) print "The average is "+ average(scores) print "The high score is" + high(scores) print "The low score is" + low(scores) print "The median is " +median(scores) print "The mode is " +mode(scores) main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.