Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #66781

Re: The sum of numbers in a line from a file

References <882091da-a499-477e-8f50-c5bdde7cdfec@googlegroups.com> <le5bhe$461$1@reader1.panix.com> <9036049f-7d08-4de7-83f8-61e5fa2c2d2f@googlegroups.com>
Date 2014-02-20 19:56 +0100
Subject Re: The sum of numbers in a line from a file
From Chris “Kwpolska” Warrick <kwpolska@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.7202.1392922619.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Feb 20, 2014 at 7:16 PM,  <kjakupak@gmail.com> wrote:
> What I've got is
> def stu_scores():
>     lines = []
>     with open("file.txt") as f:
>         lines.extend(f.readlines())
>     return ("".join(lines[11:]))

This returns a string, not a list.  Moreover, lines.extend() is
useless. Replace this with:

def stu_scores():
    with open("file.txt") as f:
        lines = f.readlines()
    return lines[11:]

> scores = stu_scores()
> for line in scores:

`for` operating on strings iterates over each character.

>     fields = line.split()

Splitting one character will turn it into a list containing itself, or
nothing if it was whitespace.

>     name = fields[0]

This is not what you want it to be — it’s only a single letter.

>     sum1 = int(fields[4]) + int(fields[5]) + int(fields[6])

Thus it fails here, because ['n'] has just one item, and not nine.

>     sum2 = int(fields[7]) + int(fields[8])
>     average1 = sum1 / 3.0
>     average2 = sum2 / 2.0
>     print ("%s %f %f %") (name, average1, average2)
>
> It says that the list index is out of range on the sum1 line. I need stu_scores because the table from above starts on line 11.
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Chris “Kwpolska” Warrick <http://kwpolska.tk>
PGP: 5EAAEA16
stop html mail | always bottom-post | only UTF-8 makes sense

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

The sum of numbers in a line from a file kxjakkk <kjakupak@gmail.com> - 2014-02-20 08:22 -0800
  Re: The sum of numbers in a line from a file John Gordon <gordon@panix.com> - 2014-02-20 16:46 +0000
    Re: The sum of numbers in a line from a file kjakupak@gmail.com - 2014-02-20 10:16 -0800
      Re: The sum of numbers in a line from a file Joel Goldstick <joel.goldstick@gmail.com> - 2014-02-20 13:32 -0500
        Re: The sum of numbers in a line from a file kjakupak@gmail.com - 2014-02-20 10:37 -0800
          Re: The sum of numbers in a line from a file Peter Otten <__peter__@web.de> - 2014-02-20 19:59 +0100
      Re: The sum of numbers in a line from a file Chris “Kwpolska” Warrick <kwpolska@gmail.com> - 2014-02-20 19:56 +0100
      Re: The sum of numbers in a line from a file MRAB <python@mrabarnett.plus.com> - 2014-02-20 19:17 +0000
  Re:The sum of numbers in a line from a file Dave Angel <davea@davea.name> - 2014-02-20 11:54 -0500
  Re: The sum of numbers in a line from a file Travis Griggs <travisgriggs@gmail.com> - 2014-02-20 14:35 -0800
  Re: The sum of numbers in a line from a file Johannes Schneider <johannes.schneider@galileo-press.de> - 2014-02-21 09:16 +0100
  Re: The sum of numbers in a line from a file sffjunkie@gmail.com - 2014-02-24 03:48 -0800
    Re: The sum of numbers in a line from a file sffjunkie@gmail.com - 2014-02-24 04:02 -0800

csiph-web