Reputation: 113
Whenever I start my game and let's say for example I enter 1,4 as the grid reference it will reply with
"UnboundLocalError: local variable 'of' referenced before assignment"
How can I fix this?
Code (edited to for brevity):
import time
while 1==1:
cont=1
while cont==1:
of="-"
tf="-"
rf="-"
ov="-"
tv="-"
rv="-"
os="-"
ts="-"
rs="-"
go=1
...
Upvotes: 1
Views: 4851
Reputation: 77910
Variable of (and all the other 2-letter variables) are not available in function xturn.
I strongly recommend that you use incremental programming: write a few lines of code, get those working, and then enlarge your program. This allows you to zero in on errors quite easily. Any time I attack a new style of programming, I find this quite useful. It appears that you're new to some of the techniques you're using here.
For instance, you're using in instead of == for comparisons. This is not going to work well as a general principle.
Declare your functions before the main program. The way you wrote this, you're redefining your functions every time you go through the loop. Moving functions to the top will cure you of many variable scoping problems, too.
Learn to use Boolean values and variables. Your loops should look like this:
while True:
cont = True
while cont:
You make variables available by passing them as arguments to the function. I can see that you're new to this, because you've given this function a parameter x that you never use.
Overall, you should not have 9 variables: you should have a list, and then just pass the entire list as the current state of the game board. If you number the squares 0-8, you can easily work with the board in that respect.
To solve the immediate problem, you can add this line to each of your routines:
global of,tf,rf,ov,tv,rv,os,ts,rs
This will make the variables available. I see that @Thomas has pointed this out.
Still, I encourage you to work on designing this more cleanly. Using global variables is generally bad design. Also, notice how much code you have to duplicate for this program? It should be a lot easier.
Upvotes: 2
Reputation: 5323
Accessing a variable inside one of your functions will basically work even if the variable is defined only in an outside scope. But in your xturn
function, you have a line that assigns to of
. It doesn't matter whether this line has been executed before the error occurs; its mere existence anywhere inside the function causes the Python interpreter to treat it as a local variable. Therefore, when accessing it in the if
clause, Python tries to access a local variable of
and now it does matter that such a local variable hasn't been assigned up until that point.
Upvotes: 2
Reputation: 10126
In your function xturn
you use the variable of
which is not declared / known in that scope, thus the error.
If you look at the stack trace:
Traceback (most recent call last):
File "D:/Projekte/Python/snakes35/blabla.py", line 141, in <module>
ocheck(1)
File "D:/Projekte/Python/snakes35/blabla.py", line 138, in ocheck
xturn(1)
File "D:/Projekte/Python/snakes35/blabla.py", line 33, in xturn
if goo in "1,4" and of not in "o":
UnboundLocalError: local variable 'of' referenced before assignment
you can figure that out via looking at the lines in your file, where the error ocures.
Upvotes: 0