need help writing this in python. 3.4 ?4 ?? COP 1ooo Exam 2, Part 2 The Problem
ID: 3715684 • Letter: N
Question
need help writing this in python. 3.4
?4 ?? COP 1ooo Exam 2, Part 2 The Problem write a Python students as desired into an array. program that allows the user to enter as many sets of five grades for as many ritten to a file. After all sets of grade ated and displayed or each set of grades, an average grade should be calculated from the file. Aclass processed, the averages should be read average should be calculated displayed. Requirements Your Python program should contain each of the following e A -A functio main module that implements pseudo-code on the following page (20 points) s the user to enter n enterGrades that is called from the main module and allow five grades between 0 and 100 and and store each grade in a list called points). The list should be returned to def main) A function calcAverage th in myGrades. The average of the set of grades should be written to a file at calculates and displays the average of the five grades stored averageGrades.dat. (20 points) After all sets of grades have been entered, a function readAverages that read average from averageGrades.dat and accumulates the sum of all average sum should be returned to def main(). (20 points) o A function classAverage that calculates and displays the class averageExplanation / Answer
file_name = 'averageGrades.dat'
def enterGrades():
myGrades= []
myGrades.append(float(raw_input('Enter grade 1: ')))
myGrades.append(float(raw_input('Enter grade 2: ')))
myGrades.append(float(raw_input('Enter grade 3: ')))
myGrades.append(float(raw_input('Enter grade 4: ')))
myGrades.append(float(raw_input('Enter grade 5: ')))
return myGrades
def calcAverage(myGrades):
sum = 0
for g in myGrades:
sum+=g
avg = sum/len(myGrades)
with open(file_name) as in_file:
in_file.write(avg)
def readAverages():
sumAverages=0
f = open(file_name)
lines = f.readlines()
noOfAvg=len(lines)
for line in lines:
averages+=float(line)
return sumAverages, noOfAvg
def classAverage(sumAverages, noOfAvg):
print 'Class average: {}'.format(sumAverages/noOfAvg)
def main():
grades = enterGrades()
calcAverage(grades)
sumAverages, noOfAvg = readAverages()
classAverage(sumAverages, noOfAvg)
if __name__ == '__main__':
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.