Question

In: Computer Science

Write a pyhton program to read from a file the names and grades of a class...

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

Solutions

Expert Solution

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:


Related Solutions

Write a python program to read from a file the names and grades of a class...
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....
Write a program in python to read from a file the names and grades of a...
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...
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
Write a program to read in a list of domain names from the provided file (domains.dat),...
Write a program to read in a list of domain names from the provided file (domains.dat), and print the reverse domain names in sorted order. For example, the reverse domain of cs.princeton.edu is edu.princeton.cs. This computation is useful for web log analysis. To do so, create a data type Domain, using reverse domain name order. The H file contains the required functions. The compareTo function is provided for you. This will allow you to compare 2 Domain objects. Also use...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
C++ Write a program that reads candidate names and numbers of votes in from a file....
C++ Write a program that reads candidate names and numbers of votes in from a file. You may assume that each candidate has a single word first name and a single word last name (although you do not have to make this assumption). Your program should read the candidates and the number of votes received into one or more dynamically allocated arrays. In order to allocate the arrays you will need to know the number of records in the file....
You have to write a program that will read an array from a file and print...
You have to write a program that will read an array from a file and print if the numbers in the file are right truncatable primes. A right truncatable prime is a prime number, where if you truncate any numbers from the right, the resulting number is still prime. For example, 3797 is a truncatable prime number number because 3797, 379, 37, and 3 are all primes. Input-Output format: Your program will take the file name as input. The first...
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
Python DESCRIPTION Write a program that will read an array of integers from a file and...
Python DESCRIPTION Write a program that will read an array of integers from a file and do the following: ● Task 1: Revert the array in N/2 complexity time (i.e., number of steps) . ● Task 2: Find the maximum and minimum element of the array. INPUT OUTPUT Read the array of integers from a file named “ inputHW1.txt ”. To do this, you can use code snippet from the “ file.py ” file. This file is provided in Canvas....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT