Problem with sum of values from .txt file
 Problem with sum of values from .txt file PathhK Unladen Swallow Posts: 1 Threads: 1 Joined: Jan 2019 Reputation: Jan-07-2019, 07:11 PM (This post was last modified: Jan-07-2019, 07:34 PM by buran.) Hi My task is to count difference between max and min value in each lines from .txt file then sum all these values. I've done the first part(the code below) but I don't know how best is it to sum all these values(I tried many ways). I'll be grateful for help ```filepath = 'rows.txt' with open(filepath) as fp: line = [int(num) for num in fp.readline().split()] cnt = 1 while line: x=max(line)-min(line) print("The difference between max-min value in line",cnt,"is :",x) cnt += 1 #print(sum(x)) #print(cnt) line = [int(num) for num in fp.readline().split()]```The result of this code: ``````Output:The difference between max-min value in line 1 is : 2574 The difference between max-min value in line 2 is : 1088 The difference between max-min value in line 3 is : 1215 The difference between max-min value in line 4 is : 3852 The difference between max-min value in line 5 is : 1222 The difference between max-min value in line 6 is : 4324 The difference between max-min value in line 7 is : 1121 The difference between max-min value in line 8 is : 2465 The difference between max-min value in line 9 is : 2956 The difference between max-min value in line 10 is : 169 The difference between max-min value in line 11 is : 4644 The difference between max-min value in line 12 is : 992 The difference between max-min value in line 13 is : 1655 The difference between max-min value in line 14 is : 1422 The difference between max-min value in line 15 is : 1659 The difference between max-min value in line 16 is : 662`````` Reply Posts: 7,605 Threads: 133 Joined: Sep 2016 Reputation: Jan-07-2019, 07:35 PM (Jan-07-2019, 07:11 PM)PathhK Wrote: I tried many waysSo, what did you tried? Post your best attempt If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein How to Ask Questions The Smart Way: link and another link Create MCV example Debug small programs Reply nilamo Last Thursdayist Posts: 3,443 Threads: 99 Joined: Sep 2016 Reputation: Jan-07-2019, 07:40 PM `line` is a list of the ints in the line. So if you create a new variable, maybe `total` outside the while loop, you can do: `total += sum(line)`. Also, why are you using a while loop? Iterating over the file directly is cleaner:```with open("thefile") as fp: total = 0 for cnt, line in enumerate(fp): nums = [int(num) for num in line.split()] total += sum(nums) diff = max(nums) - min(nums) print(f"The difference between max-min value in line {cnt} is: {diff}") print(f"The total is : {total}")``` Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Printing x values from an csv file hobbyist 7 1,342 Mar-10-2021, 02:00 PM Last Post: hobbyist Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 850 Dec-17-2019, 08:00 PM Last Post: jasonashaw problem returning values Naito 7 1,665 Jan-19-2019, 08:21 PM Last Post: Naito Problem witrh else and elif values. anolibal 7 4,118 Aug-20-2018, 11:50 PM Last Post: Skaperen Replace null values in Json file burnsssss 11 19,307 Jun-11-2018, 09:45 AM Last Post: burnsssss Getting values from Arduino into python and saving it as csv file Yamin 1 4,914 Mar-20-2018, 05:45 AM Last Post: woooee parsing values and writing back in xml file deepa 4 2,358 Sep-11-2017, 09:07 AM Last Post: deepa trying to get the keystrokes values to file rwahdan 2 2,441 Jul-14-2017, 06:53 PM Last Post: rwahdan Clean file with missing values Felipe 8 3,486 Jan-23-2017, 05:31 PM Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020