Write a .py program for the csv file given below. Your program should prompt use
ID: 3756345 • Letter: W
Question
Write a .py program for the csv file given below.
Your program should prompt users to enter a csv files as an input and the display should follow that in figure 1.
Your program should find the (i) average grade on the assignment (ii) highest and lowest grade
(ii) a histogram of the letter grades (A to F). save it as dictionary. The grade histogram should
To calculate min, max and average, use a function is better than writing own loop.
1 Student Grade 2 Bess Cain 3 Ethel Vega 4 Frances Ya 5 Edith Fulle 6 Jackson Je 7 Mable Kel 8 Jon Schwa 9 Ollie Brow 10 Hester Sim 20 21 29 93 42 27 24 83 21 Please enter a file name: Assignment3 grades.csv This assignment has an average grade of 45 This assignment's highest grade is 92 This assignment's lowest grade is 4Explanation / Answer
# This programs is written for python version greater than 3.0
import csv
import statistics
def task1():
filename = input("Please enter a file name: ") #Enter File Name
with open(filename) as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
grades = [] #define list
for row in readCSV:
grades.append(row[1])
grades.remove('Grade') #remove Grade from list
grades = list(map(int, grades)) #Convert str list to int list
print("This assignment has an average grade of", statistics.mean(grades))
print("This assignment's highest grade is", max(grades))
print("This assignment's lowest grade is", min(grades))
def task2():
import pickle
import os.path
from os import path
L = 'ABCDEF'
hist = {} #Dictionary used to store histogram
for x in L: hist[x] = hist.pop(x,0) + 1
filename = "student.pickle" # File Name hard coded
if(path.exists(filename)):
fileObject = open(filename,'wb') # open the file for writing
pickle.dump(hist,fileObject) # this writes the object a to the
fileObject.close() # here we close the fileObject
else:
print("File does not exist")
task1()
task2()
--------------------------------------------------
Output of program:
Please enter a file name: Book3.csv
This assignment has an average grade of 40
This assignment's highest grade is 93
This assignment's lowest grade is 20
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.