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

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....
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
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...
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
Write a Python program that uses function(s) for writing to and reading from a file: a....
Write a Python program that uses function(s) for writing to and reading from a file: a. Random Number File Writer Function Write a function that writes a series of random numbers to a file called "random.txt". Each random number should be in the range of 1 through 500. The function should take an argument that tells it how many random numbers to write to the file. b. Random Number File Reader Function Write another function that reads the random numbers...
build a python program that will be performing: - Read a CSV file 'annual.csv' enterprise into...
build a python program that will be performing: - Read a CSV file 'annual.csv' enterprise into a data structure - Count the number of rows and columns - Determine if the data contains empty values - Replace the empty values by 'NA' for strings, '0' for decimals and '0.0' for floats - Transform all Upper case characters to Lower case characters - Transform all Lower case characters to Upper case characters - save back the 'repaired' array as csv -...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT