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

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...
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....
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...
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....
IN PYTHON: Write a program that displays the lines from the total.txt file in the following...
IN PYTHON: Write a program that displays the lines from the total.txt file in the following output. Use a try catch phrase to check for errors. Use only one function for this portion [main()]. Remember to use Python. Sample output below: 19 16, 29 3, 30 4, 34
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT