Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'subject:file': 0.07; 'utf-8': 0.07; 'item,': 0.09; 'moreover,': 0.09; '\xe2\x80\x94': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'character.': 0.16; 'iterates': 0.16; 'itself,': 0.16; 'line.split()': 0.16; 'splitting': 0.16; 'whitespace.': 0.16; 'index': 0.16; 'wrote:': 0.18; '3.0': 0.19; 'thu,': 0.19; 'starts': 0.20; 'feb': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'replace': 0.24; 'string,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; "i've": 0.25; '2.0': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'character': 0.29; 'thus': 0.29; 'message- id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; 'url:mailman': 0.30; 'lines': 0.31; 'letter.': 0.31; 'subject:numbers': 0.31; 'says': 0.33; 'url:python': 0.33; 'table': 0.34; 'sense': 0.34; 'subject:from': 0.34; 'received:google.com': 0.35; 'url:listinfo': 0.36; 'url:org': 0.36; 'operating': 0.37; 'turn': 0.37; 'list': 0.37; 'list.': 0.37; 'pm,': 0.38; 'url:mail': 0.40; 'range': 0.61; 'from:charset:utf-8': 0.61; 'name': 0.63; 'subject:The': 0.64; 'skip:\xe2 10': 0.65; 'to:addr:gmail.com': 0.65; '20,': 0.68; 'containing': 0.69; '11.': 0.74; '8bit%:43': 0.74; 'it\xe2\x80\x99s': 0.84; 'scores': 0.91; 'url:tk': 0.95 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=2A3KXvpw3/YV7flDHMuVS0x6QWPQbQQoh0TG8pQecL4=; b=InO/eBuCmFHeeduRl2rVNIL+y+Cgil1XJZ6IuRvqU3NsWeLWSbcGw3UWWF5yoxp6E/ Jj8Erle1QUtMKah6+3QZpGvs9Z5ABaJRDTGbG1cM9tU+1rr+2LzGMLH9dAKWY92xUk6l GRQFJ74vPJacoGlsw2je/Yn83PwjB6JJ/Ip3nPF7So5//tZ5uXOiwecrtYL3/r7eASvP UBF5E6u/Pq1QcGd+/XV9VnzIcsuFHO6UygW6vpkCoCXZdNA48m3WebsyyKclTpCeohxw hJyQcNVMVhtHvfYMtzyeKy+tImImBluw1ofdJ/tulWOlW4fwcmE3mHyiCmYcD0GLGVxK jVow== MIME-Version: 1.0 X-Received: by 10.50.138.104 with SMTP id qp8mr8170228igb.39.1392922607538; Thu, 20 Feb 2014 10:56:47 -0800 (PST) In-Reply-To: <9036049f-7d08-4de7-83f8-61e5fa2c2d2f@googlegroups.com> References: <882091da-a499-477e-8f50-c5bdde7cdfec@googlegroups.com> <9036049f-7d08-4de7-83f8-61e5fa2c2d2f@googlegroups.com> Date: Thu, 20 Feb 2014 19:56:47 +0100 Subject: Re: The sum of numbers in a line from a file From: =?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?= To: kjakupak@gmail.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392922619 news.xs4all.nl 2856 [2001:888:2000:d::a6]:42046 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66781 On Thu, Feb 20, 2014 at 7:16 PM, wrote: > What I've got is > def stu_scores(): > lines =3D [] > 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 =3D f.readlines() return lines[11:] > scores =3D stu_scores() > for line in scores: `for` operating on strings iterates over each character. > fields =3D line.split() Splitting one character will turn it into a list containing itself, or nothing if it was whitespace. > name =3D fields[0] This is not what you want it to be =E2=80=94 it=E2=80=99s only a single let= ter. > sum1 =3D int(fields[4]) + int(fields[5]) + int(fields[6]) Thus it fails here, because ['n'] has just one item, and not nine. > sum2 =3D int(fields[7]) + int(fields[8]) > average1 =3D sum1 / 3.0 > average2 =3D 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 --=20 Chris =E2=80=9CKwpolska=E2=80=9D Warrick PGP: 5EAAEA16 stop html mail | always bottom-post | only UTF-8 makes sense