Python Forum
adding parts of a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
adding parts of a list
#1
Hello. I'm working on a problem where there is a file that contains the name and 5 test scores for 5 students. I have to use python 2.7 even though I know it is obsolete.
File:
Quote:pchen72 50 71 55 93 115
jmaszk 45 76 49 88 102
bvbui 59 78 53 96 145
mtcrowle 33 65 39 82 100
mrchave3 54 77 56 98 158

this is the code that I have so far:
#!/usr/bin/python
import string
from string import split
import math

num_lines = 0

with open('score') as f:

        for line in f.readlines():
                data=line.split()
                num_lines += 1
                scores1 = int(data[1])
                scores2 = int(data[2])
                grade = scores1 + scores2


print ("Total Number of Records: %i") %num_lines
print(grade)

The part I'm having trouble with is
scores1 = int(data[1])
scores2 = int(data[2])
Sure, I could do this with all 5 scores and do the math required for the problem that way but what I was trying to do was clean it up with something like this:
score = int(data[1:]
but I keep getting all types of errors. I even tried
data[1:5]
and still doesn't work. Any ideas?
Reply


Messages In This Thread
adding parts of a list - by Eric7Giants - Nov-16-2019, 08:40 PM
RE: adding parts of a list - by buran - Nov-17-2019, 08:37 AM
RE: adding parts of a list - by ichabod801 - Nov-17-2019, 02:35 PM
RE: adding parts of a list - by Eric7Giants - Nov-17-2019, 04:37 PM
RE: adding parts of a list - by buran - Nov-17-2019, 05:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  help with adding duplicates elements together in a list 2ECC3O 5 2,235 Sep-10-2022, 07:11 AM
Last Post: 2ECC3O
  Python Adding +1 to a list item cointained in a dict ElReyZero 1 2,175 Apr-30-2020, 05:12 AM
Last Post: deanhystad
  Adding values to list and pickling mefiak 2 2,931 May-31-2018, 08:57 AM
Last Post: mefiak
  adding a number to the list atux_null 4 4,056 Nov-06-2017, 07:01 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

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