Question

In: Computer Science

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. 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

Solutions

Expert Solution

(*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:


Related Solutions

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...
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....
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...
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....
Goal: to write a Python program that will read a playlist from a CSV file and...
Goal: to write a Python program that will read a playlist from a CSV file and display it in the console in the form of a table. https://s3.eu-west-2.amazonaws.com/bb-python-modules/Coursework/CW3/playlist_text_question.html The following is a link to the question. It includes all instruction and a template file (with additional instructions) where the answer must be completed.
Step by step in python please Write a program this will read a file (prompt for...
Step by step in python please Write a program this will read a file (prompt for name) containing a series of numbers (one number per line), where each number represents the radii of different circles. Have your program output a file (prompt for name) containing a table listing: the number of the circle (the order in the file) the radius of the circle the circumference the area of the circle the diameter of the circle Use different functions to calculate...
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT