-8

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)
       x = line.split(",")

       a = x[0]

       b = x[1]

       c = len(b)-1
       b = b[0:c]

       highest[a] = b

And this is the Traceback error message in full:

 Traceback (most recent call last):
        File "C:/Python34/my boto snaky/snaky.py", line 568, in gameLoop
        reader()
        File "C:/Python34/my boto snaky/snaky.py", line 531, in reader
        b = x[1]
        IndexError: list index out of range
5
  • 1
    Some of your lines in scores.txt don't have a comma (',') in them. Also, use a better title.
    – Sildar
    Jan 12, 2016 at 13:27
  • 2
    Is that really the most descriptive title you could come up with?
    – David
    Jan 12, 2016 at 13:28
  • 1
    it says the error. "list index out of range". You either dont have commas in some of your lines or missing data.
    – Chris
    Jan 12, 2016 at 13:28
  • actually they do have commas i think the issue is the empty lines in between each scores but i really don't know how to fix that Jan 12, 2016 at 13:33
  • 1
    @AnihsEmma: "actually they do have commas" and "the issue is the empty lines" are mutually exclusive statements. An empty line wouldn't have commas, hence the error.
    – David
    Jan 12, 2016 at 13:36

1 Answer 1

2

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
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.