Complete the following activities using Python programming
language. Use separate functions for input, each type of processing, and
output. Avoid global variables by passing parameters and returning results.
Create test data to validate the accuracy of each program. Include references
to any resources used. Note: Each of the following activities uses code only to
read the file. It is not necessary to use code to create the file.
1.
Using a text editor or
IDE, copy the following list of names and grade scores and save it as a text
file named scores.txt:
Name,Score
Joe
Besser,70
Curly Joe
DeRita,0
Larry
Fine,80
Curly
Howard,65
Moe
Howard,100
Shemp
Howard,85
Create a program that displays high, low, and
average scores based on input from scores.txt. Verify that the file exists and then use string
functions/methods to parse the file content and add each score to an array.
Display the array contents and then calculate and display the high, low, and
average score. Format the average to two decimal places. Include error handling
in case the file is formatted incorrectly. Note that the program must work for
any given number of scores in the file. Do not assume there will always be six
scores.
2.
import os
3.
4.
def process_file(filename):
5.
f = open(filename,'r')
6.
lines = f.readlines()[1:]
7.
f.close()
8.
9.
scores = []
10.
11. for line in lines:
12. parsed = line.split(",")
13. count = int(parsed[1])
14. scores.append(count)
15.
16. calculate_result(scores)
17.
18.def calculate_result(scores):
19. print("High: ", max(scores))
20. print("Low: ", min(scores))
21. print("Average: ", sum(scores)/len(scores))
22.
23.def main():
24. filename = "scores.text"
25. if os.path.isfile(filename):
26. process_file(filename)
27. else:
28. print ("File does not exist")
29. return 0
30.
31.main()
Get Free Quote!
416 Experts Online