Q:

Why do I have an error on my code with my files?

So the lines of the file mystery.txt contain either the word UP or DOWN or a pair of numbers. UP and DOWN are instructions for a turtle to lift up or put down its pen. The pair of numbers are some x,y-coordinates. The first few lines of mystery.txt are
UP
-218 185
DOWN
-240 189
-246 188
-248 183

The error is in my else statement that says: 
sammy.goto(wordList[i],wordList[i+1])

I want to know why I am not supposed to use [i+1] in this particular case and how to fix it. Below is my code...
import turtle
turtle.setup(800,600) # Change the width of the drawing to 800px and the height to 600px.
wn = turtle.Screen()
sammy = turtle.Turtle()

inFile = open('mystery.txt','r')
fileString = inFile.read()   # read entire file into a string
inFile.close()   # we're done with the file, so we can close it now
wordList = fileString.split()

for i in range(len(wordList)):
    if str(wordList[i]) == "UP":
        sammy.penup()

    elif wordList[i] == "DOWN":
        sammy.pendown()

    else:
        sammy.goto(wordList[i],wordList[i+1])
        i += 1

The error is on line 15 of the code directly after the else statement. The error states: 
IndexError: list index out of range on line 15

A:

Here is a cleaner way to do it.
We iterate on the lines of the file one by one, and treat each one when we read it. 
When we have a line of coordinates, we split it and convert the values to int.
import turtle
turtle.setup(800,600) # Change the width of the drawing to 800px and the height to 600px.
wn = turtle.Screen()
sammy = turtle.Turtle()

with open('mystery.txt','r') as inFile:  # using with will take care of closing the file, whatever happens 
    for line in inFile:  # read and treat the file line by line
        line = line.strip()  # remove the trailing '\n'

        if line == "UP":
            sammy.penup()

        elif line == "DOWN":
            sammy.pendown()

        else:
            # we have a pair of coordinates, we split the line and convert the coords to int
            x, y = map(int, line.split())
            sammy.goto(x, y)

Note that you most of the time don't need to use indices when iterating in Python, which avoids the kind of problem you met. 

