QUESTION: Complete the following program, which reads in a file that has multiple grades, each separated by a comma, and prints out the computed average. That is, write the functions getGrades():
and calculateAverage():
def main():
grades = getGrades() #get the file name containing the grades
#and return the contents of the file
avg = calculateAverage(grades) #separate the grades into numbers and compute
#the average
print("The calculated average is:", avg)
main()
I have inserted the given function and wrote a new program, but I am little bit confused, also I am getting an error. Please help!
def getGrades():
filename = input("Please enter a file name: ")
openfile = open(filename, "r")
readfile = openfile.readlines()
return readfile
def calculateAverage(n):
totalGrades = []
for i in (n):
Split = list(map(int, i.split(",")))
totalGrades += Split
avg = sum(totalGrades)/float(len(totalGrades))
return avg
def main():
grades = getGrades()
avg = calculateAverage(grades)
print("The calculated average is:", avg)
main()