In: Computer Science
Write a pyhton 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 dataInput() 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 dataStats() that iterates over the list or 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 dataEval() that will iterate over the dictionary or list 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 : Galia, 96 -Elias, 81 -Hussein, 84 -Joseph, 80 -Tarek, 93 -Joe, 81 -Fidele, 71 -Shant, 95 -Ali, 67 -Firas, 70 -Giorgiou, 55 -Hashem, 72
Python Code:
# dataInput function with the file name as the parameter
def dataInput(fileName):
f = open(fileName, "r"); # Reads data from the file
# The value read is stored in file variable as an array. eg. ["Galia, 96", "Elias, 81", ...]
file = f.readline().split(" -");
f.close()
# dictionary variable of type dict is initialized
dictionary = dict();
# Students names are stored as key and grades are stored as values in dictionary variable
for value in file:
key_value = value.split(", ");
dictionary[key_value[0]] = key_value[1];
return dictionary; # eg. {'Galia': '96', 'Elias': '81', 'Hussein': '84'....}
# dataStats function with the dictionary output from the dataInput function as the parameter
def dataStats(dictionary):
length = len(dictionary); # length hold the number of values in dictionary
sumValue = 0; # sumValue is initialized
# Alternate method to find minimum and maximum values
#maximum = max(dictionary.values());
#minimum = min(dictionary.values());
maxLoop = 0;
minLoop = 100;
# Iterates through each value in the dictionary
for key, value in dictionary.items():
sumValue += int(value); # sumValue is found for each value in the dictionary
if(int(value) > maxLoop):
maxLoop = int(value); # maximum value is found
if(int(value) < minLoop):
minLoop = int(value); # minimum value is found
average = sumValue / length; # average is found with sumValue and length
# eg. ['Average = 78.75', 'Minimum Grade = 55', 'Maximum grade = 96']
return ["Class Average = " + str(average), "Minimum Grade = " + str(minLoop),
"Maximum grade = " + str(maxLoop)];
# dataEval function with the dictionary and stats outputs from the dataInput
# and dataStats function as the parameter
def dataEval(dictionary, stats):
passingStudents = []; # passingStudents holds student names with grade >= 60
failingStudents = []; # failingStudents holds student names with grade < 60
# finalString holds the final string value which is used to write to the Class_Results.txt
finalString = "";
# headers hols the message to be displayed
headers = ["Passing Students", "Failing Students", "Class Statistics"];
# Iterates through each value in the dictionary
for key, value in dictionary.items():
# If the grade >= 60, the names of the student is added to the passingStudents list
if(int(value) >= 60):
passingStudents.append(key);
# If the grade < 60, the names of the student is added to the failingStudents list
else:
failingStudents.append(key);
# All the lists are stored in output_array
output_array = [passingStudents, failingStudents, stats];
for each_array_index in range(len(output_array)):
# header message is added to the finalString
finalString += headers[each_array_index] + "\n";
for each_value_each_array in output_array[each_array_index]:
# Values are added to finalString
finalString += (each_value_each_array) + "\n";
finalString += "\n";
# A new file Class_Results.txt is created
file = open("Class_Results.txt", "w");
# Write value in finalString to ClassResults.txt
file.write(finalString)
file.close()
# Main Function executes dataInput(), dataStats() and dataEval()
def main():
print('''This is a 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.''');
# Gets filename from the user
filename = input("Enter the name of the file: ");
#dictionary = dataInput("Class_Data.txt");
dictionary = dataInput(filename); #function call: dataInput()
stats = dataStats(dictionary); #function call: dataStats()
dataEval(dictionary, stats); #function call: dataEval()
main();
INPUT FILE (Class_Data.txt)
OUTPUT: