In: Computer Science
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions:
a) Develop a getGrades() function that reads data from a file and stores it and returns it as a dictionary. The function gets the file name as an argument, reads the data, and returns a dictionary. Each line on the file should contain the record of one student holding his or her name and the corresponding grade.
b) Write a second function statsGrades() that iterates over the dictionary and determines the statistics on the average, minimum, and maximum of the grades and return these statistics as a list.
c) Write a third function reportGrades() that will iterate over the dictionary and write on a new file named Class_Results.txt the names of students whose grades are greater or equal to 60, preceded by the message “Passing Students”. Then the names of students whose grades are lower than 60 should written on the file, preceded by the message “Failing Students”. Finally, the class statistics should be written on the file, also preceded by the message “Class Statistics”.
d) Write a main program that coordinates the operation of the three developed functions. The program should inform the user of what is its function and request from the user the name of the file where class names and grades are written. Form you own data file, Class_Data.txt that have the following entries :
Majdi, 95 Tarek, 56 Jad, 77 Youssef, 97 Zahra, 86 Joe, 81 Fidele, 71 Mosbah, 98 Emile, 76 Aline, 68 Nicolas, 73 Ahmad, 53
Complete code in Python3:-
# This function takes filenme
# Open file and reads its content, store them in dictionary and
returns dic.
def getGrades(filename):
fp = open(filename, 'r')
data = fp.readlines()
students = dict({})
for line in data:
student_data = line.split(",
")
students[student_data[0]] =
float(student_data[1])
fp.close()
return students
# This function calculates Min, Max, Average od data.
def statsGrades(grades):
Max = 0
Min = 10000
total = 0
n = 0
for key in grades.keys():
total = grades[key]
if grades[key] > Max:
Max =
grades[key]
if grades[key] < Min:
Min =
grades[key]
n += 1
avg = total/n
return[Min, Max, avg]
# This function is saving data in new file.
def reportGrades(grades, stats):
fp = open("Class_Result.txt", 'w')
for key in grades.keys():
marks = grades[key]
if marks >= 60:
result =
"Passing Students"
fp.write(key+",
"+str(marks)+", "+result+"\n")
for key in grades.keys():
marks = grades[key]
if marks < 60:
result =
"Failing Students"
fp.write(key+",
"+str(marks)+", "+result+"\n")
fp.write("\n"+"Class Statistics"+"\n")
fp.write("Min: "+str(stats[0])+", Max:
"+str(stats[1])+", Average: "+str(stats[2])+"\n")
fp.close()
# Main code
# Taking file as user input from user.
filename = input("Enter file name contains class data : ")
# Calling getGrades() function for getting data from file.
grades = getGrades(filename)
# Calling statsGrades() function for getting data stats
stats = statsGrades(grades)
# Calling reportGrades() function
reportGrades(grades, stats)
Screenshot of input file:-
Screenshot of output file:-