0
import turtle

ben = turtle.Turtle()
W = 1
ben.speed(10.5)
ben.width(5)
#X = 0
O = 0



if GRIDDONE == True: #inside lines
    ben.speed(10.5)
    ben.forward(100)
    ben.right(90)
    ben.forward(300)
    ben.left(90)
    ben.forward(100)
    ben.left(90)
    ben.forward(300)
    ben.right(90)
    ben.forward(100)
    ben.right(90)
    ben.forward(100)
    ben.right(90)
    ben.forward(300)
    ben.left(90)
    ben.forward(100)
    ben.left(90)
    ben.forward(300)

ben.pu()
ben.forward(500)

X = turtle.Turtle()
O = turtle.Turtle()
X.pu()
O.pu()

X.forward(300)
X.right(90)
O.forward(300)
O.right(90)

X.color("Blue")
X.color()
O.color("Red")
O.color()
X.width(2)
O.width(2)
#X.speed(100)
#O.speed(100)

SQ1 = False


StartX = '300'
StaryY = '0'


def playX():
    P1 = input('Can player X please choose their square')

    if (P1) == ('1'):
        if (SQ1) == False:
            X.right(90)
            X.forward(300) #change this
            X.left(90)
            X.left(45)
            X.pd()
            X.forward(140)
            X.pu()
            X.left(135)
            X.forward(100)
            X.left(135)
            X.pd()
            X.forward(140)
            X.pu()
            X.setx(300)
            X.sety(0)
            X.left(45)
            SQ1 = True




def playO():
    P1 = input('Can player O please choose their square')

    if P1 == ('1'):
        if SQ1 == False:
            O.right(90)
            O.forward(300) #change this
            O.left(90)
            O.forward(50)
            O.left(90)
            O.forward(10)
            O.left(90)
            O.pd()
            O.circle(-40)
            O.pu()
            O.setx(300)
            O.sety(0)
            O.left(180)
            SQ1 = True


playO()
#playX()

THIS IS my code and i need to create a feature that doesn't allow the turtle to draw under the same 'if' function twice but i get the error in the title. The error is as follows:

Traceback (most recent call last): File "C:\Users\benna\Downloads\2.py", line 314, in playO() File "C:\Users\benna\Downloads\2.py", line 297, in playO if SQ1 == False: UnboundLocalError: local variable 'SQ1' referenced before assignment

i think its because the variable can't be edited whilst in use in the function.

  • 2
    Possible duplicate of [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – chevybow Jun 08 '18 at 18:38

1 Answers1

0

The error is due to SQ1 being declared in the global space and then being accessed inside your play0() function.

You can fix this error by using the keyword global. Add a line inside your play0() function that specifies SQ1 is global as shown below (see line 2):

def playO():
    global SQ1

    P1 = input('Can player O please choose their square')

    if P1 == ('1'):
        if SQ1 == False:

Further improvements:

I would suggest following the Python Style Guide for your variable names and to help improve the readability of the code.

Prins
  • 1,051
  • 1
  • 6
  • 9