In: Computer Science
Write a python program 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 gradesInput() function that reads data from a file and stores it and returns it as a dictionary. The function has one argument which is the name of the file where the data is entered. 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 gradesAssess() 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 gradesReport() 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 be 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 has the following entries : Raja, 90 Yahya, 50 Jad, 70 Hussein, 71 John, 97 Tony, 98 Nasser, 78 Maroun, 70 Pamela, 78 Maroun, 70 Ali, 74 Firas, 85
(*Note: Please up-vote. If any doubt, please let me know in the comments)
CODE:
def gradesInput(filename):
grades_dict = {} #create empty dictionary for holding data from file
with open(filename, "r") as f:
lines = f.readlines()
for line in lines:
line.strip() #strip away extra blank spaces from file
name, grade = line.split(",") #split each line at comma to separate name and grade
grades_dict[name] = int(grade) #convert grade from string to int
return grades_dict
def gradesAssess(grades_dict):
minimum = min(grades_dict.values())
maximum = max(grades_dict.values())
average = sum(grades_dict.values())/len(grades_dict.values())
return [average, minimum, maximum] #Return the 3 results as a list
def gradesReport(grades_dict):
with open("Class_Results.txt", "w") as fo:
fo.writelines("Passing Students:\n")
passing_students = [item[0]
for item in grades_dict.items() if item[1] >= 60] #create a list called passing students using list comprehension
for passing_student in passing_students:
fo.writelines(passing_student+"\n")
fo.writelines("Failing Students:\n")
failing_students = [item[0]
for item in grades_dict.items() if item[1] < 60] #create a list called failing students using list comprehension
for failing_student in failing_students:
fo.writelines(failing_student+"\n")
fo.writelines("Class Statistics:\n")
stats = gradesAssess(grades_dict)
fo.writelines(f"Average: {stats[0]:.2f}\n")
fo.writelines(f"Minimum: {stats[1]}\n")
fo.writelines(f"Maximum: {stats[2]}\n")
# main
print("This program will read class data from a file specified by you and then it will write the names of passing, failing students and class statistics to file called Class_Results.txt")
file_name = input("Please enter the name of the file where class data is stored: ")
class_dict = gradesInput(file_name)
stats = gradesAssess(class_dict)
print(f"Average: {stats[0]:.2f}\n")
print(f"Minimum: {stats[1]}\n")
print(f"Maximum: {stats[2]}\n")
gradesReport(class_dict)
print("Class_Results.txt file has been created successfuly")
CODE SCREENSHOT:
Output Screenshots:
Output file: