Python programming question Assume that a file containing a

Python programming question :

Assume that a file containing a series of integers is named numbers.txt. Write
a program that calculates the average of all the numbers stored in the file.

49.6

expexted result : 349.2

Solution

numbers = []
with open(\"numbers.txt\", \"r\") as ins:
for line in ins:
numbers.append(line)
sum = 0
for i in range(0, len(numbers)):
sum = sum + int(numbers[i])
average = sum/float(len(numbers))
print \"Average is \", average

OUtput:

sh-4.3$ python main.py                                                                                                                                                                                                      

Average is  5.5

numbers.txt

1
2
3
4
5
6
7
8
9
10

Python programming question : Assume that a file containing a series of integers is named numbers.txt. Write a program that calculates the average of all the nu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site