0
def reader():
     myfile = open("scores.txt","r")
     pre = myfile.readlines()

     for line in pre :

          x = line.split(",")
          a = x[0]
          b = x[1]
          c = len(b)-1c
          b = b[0:c]
          highest[a] = b

I have succeeded in writing the scores to a file and I intend to read the file into a dic, but it's not working. I'm getting:

   error: IndexError: list index out of range 

How do I fix it? anymore

AlG
  • 14,697
  • 4
  • 41
  • 54
  • pls provide the exact code that gives you the error. you can also put in print statements to see where the index error is produced. – Barney Szabolcs Jan 12 '16 at 10:52
  • and i still get an AssertionError but thanks a lot i am greatful – Anihs Emma Jan 12 '16 at 10:54
  • highest = {} def reader(): myfile = open("scores.txt","r") pre = myfile.readlines() print(pre) for line in pre : x = line.split(",") assert len(x) == 1 a = x[0] b = x[1] # c = len(b)-1 # b = b[0:c] highest[a] = b print(line) print(x[1]) – Anihs Emma Jan 12 '16 at 10:56
  • Anihs, add your code to your original question - it's easier to read there. :) – AlG Jan 12 '16 at 12:07
  • What is the line that gives the error? Is it `a = x[0]`/`b = x[1]`? If so, check your file for empty lines or lines without commas. Is it `highest[a] = b`? If so, please show us how `highest` is initialized. – Andrea Corbellini Jan 12 '16 at 12:27
  • Oh, and I see that the code you posted in the comment is not the same that you put in the question. – Andrea Corbellini Jan 12 '16 at 12:30
  • its x[1] and my files actually has empty lines but i dont know ho wto fix that – Anihs Emma Jan 12 '16 at 13:09
  • i want to write the scores into the files in different lines so trying to read it back into the highest = {} dictionary gives the error at b = x[1] – Anihs Emma Jan 12 '16 at 13:11
  • yes the codes are different someone already helped with an answer so i applied it and got another error and i was just trying to show him i already did what he asked me to do – Anihs Emma Jan 12 '16 at 13:13
  • please edit the question and: 1. include the most recent code; 2. include the full traceback for the error you are getting now – Andrea Corbellini Jan 12 '16 at 13:16
  • Possible duplicate of [Python - index out of range error](http://stackoverflow.com/questions/34744959/python-index-out-of-range-error) – Barney Szabolcs Jan 15 '16 at 14:49

0 Answers0