0

I have a lab due tonight that I have been struggling with and need help with just one step. I have a file that looks exactly like this with out the list numbers and period, but each name with its scores is on a different line

 1. johnd 20 30 12 25 50
 2. janeb 10 10 20 23 10
 3. saran 23 10 30 30 30
 4. frankb 25 21 22 30 23
 5.  marys 10 10 10 10 10

I am required to add each persons scores/numbers together and I've been trying to figure it out for hours but do not know how to separate them all to add them together. If somebody could just explain to me how to separate the lines and add the numbers together i would appreciate it greatly.
I've tried using this code but still cannot get it to work for line in h:

for i in line: 

    # Checking for the digit in  
    # the string 
    if i.isdigit() == True: 
        a += int(i)
jnovack
  • 7,629
  • 2
  • 26
  • 40
offuttm
  • 1
  • 1
  • If you have a file that looks "exactly" like something, remember to use proper markup. Putting it in a paragraph as plain text is not [asking as good a question as you can](/help/how-to-ask) – Mike 'Pomax' Kamermans Oct 08 '20 at 00:11

1 Answers1

0

Since you know that all fields but the first in each line are the numbers, you can use split() to separate, map() to convert to integers, and sum() to add them.

for line in h:
    parts = line.split()
    print(parts[0], sum(map(int, parts[1:])))
Armali
  • 18,255
  • 14
  • 57
  • 171