file.py
= 0
total
with open("data.txt") as f:
for line in f:
= int(line)
number += number
total
print("Sum of all values is:", total)
Starting with creating our data file data.txt
:
data.txt
12
54
7
332
54
1 0
We start by defining an integer, total
to be zero before the loop. Inside the loop we add the line of code total += number
which increases the variable total
by the value in the variable number
.
Finally we print the total:
file.py
= 0
total
with open("data.txt") as f:
for line in f:
= int(line)
number += number
total
print("Sum of all values is:", total)
python file.py
Sum of all values is: 460