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


Groups > comp.lang.python > #66783

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

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!us.feeder.erje.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.018
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'subject:file': 0.07; 'def': 0.12; 'be:': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'line.split()': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'index': 0.16; 'wrote:': 0.18; '3.0': 0.19; 'starts': 0.20; 'print': 0.22; 'header:User-Agent:1': 0.23; "i've": 0.25; '2.0': 0.26; 'header:In-Reply-To:1': 0.27; 'skip:( 20': 0.30; 'lines': 0.31; 'subject:numbers': 0.31; 'says': 0.33; 'table': 0.34; 'subject:from': 0.34; 'received:84': 0.35; 'should': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'range': 0.61; 'email addr:gmail.com': 0.63; 'name': 0.63; 'subject:The': 0.64; 'apart': 0.72; '11.': 0.74; 'scores': 0.91
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=JLW1sq6b c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=sASEtNAQL0YA:10 a=fOtvRCllXMIA:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=AioAZu2PjVcA:10 a=pGLkceISAAAA:8 a=G6jh1P-Pp5bzq8JpMBwA:9 a=5CUS0MKsm1Lw3cXc:21 a=QEXdDO2ut3YA:10 a=MSl-tDqOz04A:10
X-AUTH mrabarnett:2500
Date Thu, 20 Feb 2014 19:17:11 +0000
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0
MIME-Version 1.0
To python-list@python.org
Subject 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>
In-Reply-To <9036049f-7d08-4de7-83f8-61e5fa2c2d2f@googlegroups.com>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.7204.1392923835.18130.python-list@python.org> (permalink)
Lines 24
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1392923835 news.xs4all.nl 2871 [2001:888:2000:d::a6]:49180
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:66783

Show key headers only | View raw


On 2014-02-20 18:16, 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:]))
>
> scores = stu_scores()
> for line in scores:
>      fields = line.split()
>      name = fields[0]
>      sum1 = int(fields[4]) + int(fields[5]) + int(fields[6])
>      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.
>
Apart from the other replies, the final print is wrong. It should be:

     print "%s %f %f" % (name, average1, average2)

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