code
Python exercise: Sum values from text file and print the result
*************
Episode: 7
Level: Beginner
Let's say you have some text file and this file have different number on every line.
input.txt
1: 223.5
2: -12.1
3: 4308
4: -449.8
5: 18.9
6: 1
7: 2209
8: -1890
9: 14.45 One solution is to use list comprehensions to make a list of values and then sum the list.
Solution:
1: with open("input.txt", "r") as tf:
2: numbers = [float(line) for line in tf]
3: total = sum(numbers)
4: print(total)
With numbers above what is your result?
o_o


Post a Comment
0 Comments
Thanks for sharing your thoughts !