Skip to content Skip to sidebar Skip to footer

Python - Index Out Of Range Error

This is my most recent code: highest = {} def reader(): myfile = open('scores.txt','r') pre = myfile.readlines() print(pre) for line in pre : print(line)

Solution 1:

Some of your lines in scores.txt don't have a comma. You can check for those :

if len(x) == 1 : #there is no comma
    continue #ignore line and go to the next one

This code would ignore the lines without a comma. Place it just after computing x = line.split(',') .

Same if you just want to skip empty lines :

if line.strip() == '': #remove whitespace then check if line is empty
    continue

Post a Comment for "Python - Index Out Of Range Error"