The sum of numbers in a line from a file

Joel Goldstick joel.goldstick at gmail.com
Thu Feb 20 19:32:49 CET 2014


On Feb 20, 2014 1:20 PM, <kjakupak at 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]
Print fields here to see what's up
>     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.
> --
> https://mail.python.org/mailman/listinfo/python-list
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140220/50765269/attachment.html>


More information about the Python-list mailing list