#๐Ÿ”’ Error setting global varible

7 messages ยท Page 1 of 1 (latest)

safe gorge
#
import random
import customtkinter as ctk

gridButtons = []

window = ctk.CTk()
window.title("Tic Tac Toe")

isX = True


for x in range(0,3):         
    gridButtons.append([])
    for y in range(0,3):
        gridButtons[x].append(ctk.CTkButton(window, width = 50, height = 50, text = "", command=lambda xx=x, yy = y: played(xx,yy,isX)))
        gridButtons[x][y].place(x=x*51, y=y*51) #Placing buttons with 1px spacing

def checkGrid(player, grid):
    for i in range(0,2): 
        for x in range(0,3):    #Lines 19-29 -> Checking for horizontal & vertical wins
            tempInt = 0
            for y in range(0,3):
                if(i == 0):
                    if(grid[x][y] == player):
                        tempInt += 1
                elif(i == 1):
                    if(grid[y][x] == player):
                        tempInt += 1
            if(tempInt == 3):
                return True
        tempVint = 0            #Lines 39-39 -> Checking for diagonal wins
        for x in range(0,3):
            if(i == 0):
                if(grid[x][x] == player):
                    tempVint += 1
            elif(i == 1):
                if(grid[2-x][2-x] == player):
                    tempVint += 1
        if(tempVint == 3):
            return True
        
def played(x,y,X):
    if X == True:
        gridButtons[x][y].configure(text="X")
        isX = not(isX)
    else:
        gridButtons[x][y].configure(text="O")
        isX = not(isX)

window.mainloop()
```  it throws this error
```py UnboundLocalError: cannot access local variable 'isX' where it is not associated with a value
bright siloBOT
#

@safe gorge

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

normal vault
#

global isX
Put that at the start of the function.

#

Otherwise it tries to look for it as a local variable and ends up not finding it.

#

If you assign a variable within a function, by default it thinks it's a local variable.

bright siloBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.