The sum of numbers in a line from a file

Peter Otten __peter__ at web.de
Thu Feb 20 19:59:31 CET 2014


kjakupak at gmail.com wrote:

> Error comes up saying "IndexError: list index out of range."

OK, then the 12th line starts with a whitespace char. Add more print 
statements to see the problem:

> scores = stu_scores()

print scores

> for line in scores:

      print repr(line)

>     fields = line.split()
>     name = fields[0]
> print (fields)

Hint:

> def stu_scores():
>     lines = []
>     with open("file.txt") as f:
>         lines.extend(f.readlines())
>     return ("".join(lines[11:]))
 
Why did you "".join the lines?




More information about the Python-list mailing list