loading...
Cover image for HELP file reading in python !

HELP file reading in python !

tiwariji264 profile image Sudhanshu tiwari Updated on ・1 min read

problem :
You have the file sums.txt. It has multiple lines that contain two numbers separated by whitespace. All numbers are positive.

For example:

9 61
15 47
2 1

Write a program that reads this file and print the sum of numbers on each line. So, if the file has n lines, you should print n sums, each on a separate line.

MY code :

read sums.txt

p = []
v = []
a = open('sums.txt', 'r')
for i in a.readlines():
v += i.split()
for s in v:
if s.isdigit() is True:
p.append(int(s))
print(sum(p))
a.close()

Discussion

markdown guide
 

It would help if:

  • you would tell us what your output is.
  • you would format your code with markdown so that it's readable. Sandwich your code between two lines of 3 backticks.

To debug I'd suggest you start by printing intermediate results so you can pinpoint where it goes wrong.

 

HELP ME TO SOLVE THE PROBLEM