UnboundLocalError: local variable 'actionNumber' referenced before assignment and Array

Question:

I’m really sorry if this question has been asked before, but I can’t seem to find the solution to this error anywhere for my case.

I have a text file with screen coordinates (x and y, separated by a new line), and I want to read the file, and store every line of the file onto an two separate arrays – one for X, and one for Y.

I then want to use pyautogui.moveTo() to move to the two x and y coordinates, then click (I haven’t done any of that yet).

The variable actionNumber simply acts like i in a for loop, to count which line we’re on in the text file.

How can I create the two arrays (dataX[] and dataY[]) in python that can be read through mainLoop(), and then store a line of data from the file in dataX[actionNumber] and then the next line of the file into dataY[actionNumber]?

Here is my code, it is pretty simple:

actionNumber = 0

dataX = []
dataY = []

f = open("track_cursor_position_log.txt", "r")
#print(f.read())


def mainLoop():
    #I need to store the first line of the file into dataX, then the second into dataY, then the 3rd into dataX, and so on...

    actionNumber = actionNumber + 1

    mainLoop()

mainLoop()

Here are the errors I get:

Traceback (most recent call last):
  File "C:/Users/devel/Downloads/python-imagesearch-master/python-imagesearch-master/ClickAgentTest.py", line 17, in <module>
    mainLoop()
  File "C:/Users/devel/Downloads/python-imagesearch-master/python-imagesearch-master/ClickAgentTest.py", line 13, in mainLoop
    actionNumber = actionNumber + 1
UnboundLocalError: local variable 'actionNumber' referenced before assignment

Here is a small snippet of the data in the file track_cursor_position_log.txt, just so you can get what I’m talking about:

1858
1129
1292
1165
927
1287
1501
461
1567
709
2298
1049
1473
1244
2511
1722

I once more apologise if this is a noob question, but I am more of a c++/JS coder, and I haven’t used python in a while…

Thank you very much to everyone who can help out!

Asked By: SavvyDev06

||

Answers:

Since you are defining a function, it is treating any reference to actionNumber as a local variable, a variable that exists only within the function with no connection to the global variable actionNumber.

You can add global actionNumber as the first line to your function, which will make any reference to actionNumber in the function, a reference to the global actionNumber, not a local one. However, I suggest you define actionNumber as 0 in the function unless you need to use actionNumber in multiple difference functions/outside the function.

So, you’re fixed code would be either:

actionNumber = 0

dataX = []
dataY = []

f = open("track_cursor_position_log.txt", "r")
#print(f.read())


def mainLoop():
    #I need to store the first line of the file into dataX, then the second into dataY, then the 3rd into dataX, and so on...
    global actionNumber

    actionNumber = actionNumber + 1

    mainLoop()

mainLoop()

or

dataX = []
dataY = []

f = open("track_cursor_position_log.txt", "r")
#print(f.read())


def mainLoop():
    #I need to store the first line of the file into dataX, then the second into dataY, then the 3rd into dataX, and so on...
    actionNumber = 0

    actionNumber = actionNumber + 1

    mainLoop()

mainLoop()

Also, as an aside, actionNumber += 1 would be the same as actionNumber = actionNumber + 1

Answered By: Mike Smith

Well, there’s a few things I would do differently than your approach. But, to fix your error, you need to reference the global variable actionNumber rather than the local instance in your mainLoop namespace. Do this by adding this to your code:

def mainLoop():
    global actionNumber
    actionNumber = actionNumber + 1

Full example of code:

coords = []
f = open("track_cursor_position_log.txt", "r")
lines = f.readlines()
for item in lines:
   x,y = item.split(', ')
   coords.append([x,y])

for item in coords:
   pyautogui.moveto(item[0], item[1])
Answered By: JattCode113
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.