Reputation: 57
Hello fellow coders/codets, I have been trying to find a solution to finding the average of the contents inside a txt file and write them to the output program. so far I have got the following code to trying to find the average:>>>
elif viewclass==('class a ave'):
list_of_numbers = []
with open('class a.txt') as f:
for line in f:
if line.strip(): # this skips blank lines
list_of_numbers.append(int(line.strip()))
print ('Total '),len(list_of_numbers)
print ('Average '),1.0*sum(list_of_numbers)/len(list_of_numbers)
However when the code is run i get the following output:>>>
Total
Average
These are the two things that print out. In my file I have the following numbers to average 10 & 3 in the .txt file.
Thanks for the help :D
Upvotes: 0
Views: 74
Reputation: 76194
I'm assuming you're using Python 3.
print ('Total '),len(list_of_numbers)
print ('Average '),1.0*sum(list_of_numbers)/len(list_of_numbers)
The things you want to print need to all be inside the first parentheses pair.
print('Total ', len(list_of_numbers))
print('Average ', 1.0*sum(list_of_numbers)/len(list_of_numbers))
Upvotes: 1