Question

In: Computer Science

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:

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

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

Solutions

Expert Solution

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


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