1

I am fairly new to programming and I was wondering about some stuff within indexes. I am trying to make a program that will type out words(I've got this part of the code working) but whenever I run the code with the index in I get this error: list index out of range. I was wondering what this could mean.

thank you.

from pynput.keyboard import Key, Controller 
keyboard = Controller()

x = 0

file = open("A:\\Development\\Languages\\Python\\Programs\\NEWTEXT.txt", "r" )

for line in file:
    fields = line.split(";")

while x < 340:
    song = fields[x]
    keyboard.type(song)
    x = x + 1 

file.close()

exit()
    
  • 1
    Please provide your input. In your code you basically just store the items of the last line in the file because you overwrite `fields` in every iteration in the `for` loop. Is that intentional? Where does the number 340 come from? Are there guaranteed to be 340 elements in the `fields` list? Check the traceback of the error that you are getting, it contains helpful information. – David Wierichs Jun 20 '20 at 23:44
  • 1
    be sure that in your program there is 339 list in fields. You can check by printing and comment out rest – Kodi Learn Jun 20 '20 at 23:50
  • Index out of range usually means that you are trying to access an element of an iterable with an index that does not exist. I need more information on your code as mentioned to be able to diagnose the problem further. – griffin_cosgrove Jun 20 '20 at 23:54
  • The split command returns a List that you can iterate by using `for x in songs:` – Franke Jun 21 '20 at 04:07

4 Answers4

0

The split function returns a list. For example, in fields = split("cat;dog;mouse"), fields will be ["cat", "dog", "mouse"]. In this case, there are 3 elements and you will get an out of bounds error if you try to access anything more than fields[2].

In your case, your loop tries to access fields[339], which will yield an error if your fields list has less than 340 elements.

bagel_bite
  • 13
  • 2
0

Welcome to SO!

Your error means that you are trying to read the content of a list beyond its length. For example, if I create a list with 3 items in it:

mylist = ['a', 'b', 'c']
this_does_not_exist = mylist[4]  # this is undefined -> IndexError

Trying to read the 4th element of a list with 3 items makes no sense, it is not defined, so python throws an IndexError at you.

In your case, your line that reads songs = fields[x] must be valid for all values of x up to 339, in other words, your line must have at least that many values for your code to work. You can get the length of your list with len(fields) instead of trying to guess it and then use it like this:

for field in fields:
    # now field contains fields[x]
    keyboard.type(field)

This is considered more pythonic than using while like you did (which works as well, but requires you to write more code).

You can read more about IndexError in the python docs.

Laurent S
  • 4,106
  • 3
  • 26
  • 50
0

So assuming your NEWTEXT.txt file contents looks along the lines of:

jason;josh;christian;

you can modify your code to run independently of the number of elements like so:

from pynput.keyboard import Key, Controller 
keyboard = Controller()

with open('A:\\Development\\Languages\\Python\\Programs\\NEWTEXT.txt') as f:
    content = f.read()

for song in content.split(';'):
    keyboard.type(song)

But I would recommend sharing the content of your file to have better suited answers.

jadki
  • 482
  • 1
  • 8
  • 15
0

That error is caused when you tried to use a subscription of an index of the array, but the array is too small to supply that index, e.g:

l = [1,2,3,4] # The only available indexes are 0, 1, 2 and 3

print(l[6]) # This will trigger the index error

I'll add that opening and closing a file with like in your code is considered a bad practice, a context manager is generally more preferred.

In python, you can directly iterate through the element of an array, so you don't have to use and keep updating the index x:

from pynput.keyboard import Key, Controller 

keyboard = Controller()

with open("A:\\Development\\Languages\\Python\\Programs\\NEWTEXT.txt", "r") as file:

    fields = [line.replace(";"," ") for line in file]

    for song in fields:
        keyboard.type(song)

exit()
Red
  • 26,798
  • 7
  • 36
  • 58