file.py
= 0
total = 0
count
with open("data.txt") as f:
for line in f:
= int(line)
number += number
total += 1
count
print("Sum of all", count, "values is:", total)
Starting with creating our data file data.txt
:
data.txt
12
54
7
332
54
1 0
As before we start with defining a variable count
to start at zero. We increase it by 1
each time around the loop and print it out at the end:
file.py
= 0
total = 0
count
with open("data.txt") as f:
for line in f:
= int(line)
number += number
total += 1
count
print("Sum of all", count, "values is:", total)
python file.py
Sum of all 7 values is: 460