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
#2
you can use list comprehension
scores = [int(score) for score in data[1:]]
or map
scores = map(int, data[1:5])
then

grade = sum(scores)
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
#3
(Nov-16-2019, 08:40 PM)Eric7Giants Wrote: I have to use python 2.7 even though I know it is obsolete.

2.7 isn't obsolete yet. You've still got six weeks. Big Grin
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Nov-17-2019, 08:37 AM)buran Wrote: you can use list comprehension
scores = [int(score) for score in data[1:]]
or map
scores = map(int, data[1:5])
then
grade = sum(scores)

Thanks. It was able to add all the scores. Is there a way to get it to add the scores on each line separately? I tried putting the code into the for loop but it still just prints the total.
Reply
#5
(Nov-17-2019, 04:37 PM)Eric7Giants Wrote: I tried putting the code into the for loop but it still just prints the total.
show what have you tried
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  help with adding duplicates elements together in a list 2ECC3O 5 2,031 Sep-10-2022, 07:11 AM
Last Post: 2ECC3O
  Python Adding +1 to a list item cointained in a dict ElReyZero 1 2,071 Apr-30-2020, 05:12 AM
Last Post: deanhystad
  Adding values to list and pickling mefiak 2 2,827 May-31-2018, 08:57 AM
Last Post: mefiak
  adding a number to the list atux_null 4 3,868 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