#Read in the contents of the CSV file fin = open('exam_grades_small.csv','r') contents = fin.read() fin.close() #done with fin, so let's close it right away #Let's create a stream to output our results to fout=open('exam_grades_results.txt','w') #Recall that 'contents' holds the entire CSV file as one long string #Each grade is separated from the next by a comma #We need to separate out these grades numbers = contents.split(',') #'numbers' is now a list of all the numbers in the file #IMPORTANT: Those numbers are still strings though (Recall that we read in the file as a string) #Let's calculate the average: total = 0 for num in numbers: print() print(num,'\t',type(num)) #Note that it is a string print('Now let\'s cast to an integer:') num = int(num) #cast the string to an integer print(num,'\t',type(num)) #note that it is NOW an integer! total += num #Let's write the current status to the file: s = 'Current number:{0}\tCurrent sum:{1}'.format(num,str(total)) fout.write(s+'\n') #fout.write('Current sum: '+str(total)+'\n') #write can ONLY accept a STRING as its argument! #Now that we have the sum of all the numbers, get the average mean = total / len(numbers) mean = round(mean,1) print("Average: ", mean) fout.write('The average of all the numbers is {}'.format(str(mean))) fout.close()