Reputation: 17
I'm trying to solve the following problem: Write aprogram that reads the random numbers from the file random_numbers.txt, displays the numbers, then displays the following data: a. The total (sum) of the numbers b. The number of random numbers read from the file c. The average value of the numbers read from the file
Here's what i Have so far
sum = 0
runs = 0
avg = 0
myfile = open("random_number.txt", "r")
file_contents = myfile.read()
print(file_contents)
for i in myfile.readlines():
number = i
sum += number
print("the sum is", sum)
I don't know why the sum displays 0?
Upvotes: 0
Views: 1276
Reputation: 53
use this maybe slove your problem
sum = 0
runs = 0
avg = 0
myfile = open("random_number.txt", "r")
file_contents = myfile.readlines()
for i in file_contents:
#print(i)
number = i
sum += int(number)
print("the sum is", sum)
Upvotes: 1