Hey guys, i have been working on this for a while and global doesnt seem to be doing anything; help?

Row1 = [" "," "," "]
Row2 = [" "," "," "]
Row3 = [" "," "," "]
Grid = [Row1,Row2,Row3]

x = False


def NAC():
    def check():
            #only using this for testing
        if Grid[0][0] == 'X' and  Grid[0][1] == 'X' and Grid[0][2] == 'X':
            print("player 1 wins!")
            global x
            x = True
            #Only Using This for testing
        elif Grid[1][0] == 'X' and  Grid[1][1] == 'X' and Grid[1][2] == 'X':
            print("player 1 wins!")
            x = True
        elif Grid[2][0] == 'X' and  Grid[2][1] == 'X' and Grid[2][2] == 'X':
            print("player 1 wins!")
            x = True
        elif Grid[0][0] == 'X' and  Grid[1][0] == 'X' and Grid[2][0] == 'X':
            print("player 1 wins!")
            x = True
        elif Grid[0][1] == 'X' and  Grid[1][1] == 'X' and Grid[2][1] == 'X':
            print("player 1 wins!")
            x = True
        elif Grid[0][2] == 'X' and  Grid[1][2] == 'X' and Grid[2][2] == 'X':
            print("player 1 wins!")
            x = True
        elif Grid[0][0] == 'X' and  Grid[1][1] == 'X' and Grid[2][2] == 'X':
            print("player 1 wins!")
            x = True
        elif Grid[0][2] == 'X' and  Grid[1][1] == 'X' and Grid[0][1] == 'X':
            print("player 1 wins!")
            x = True

        if Grid[0][0] == 'O' and  Grid[0][1] == 'O' and Grid[0][2] == 'O':
            print("player 2 wins!")
            x = True
        elif Grid[1][0] == 'O' and  Grid[1][1] == 'O' and Grid[1][2] == 'O':
            print("player 2 wins!")
            x= True
        elif Grid[2][0] == 'O' and  Grid[2][1] == 'O' and Grid[2][2] == 'O':
            print("player 2 wins!")
            x = True
        elif Grid[0][0] == 'O' and  Grid[1][0] == 'O' and Grid[2][0] == 'O':
            print("player 2 wins!")
            x = True
        elif Grid[0][1] == 'O' and  Grid[1][1] == 'O' and Grid[2][1] == 'O':
            print("player 2 wins!")
            x = True
        elif Grid[0][2] == 'O' and  Grid[1][2] == 'O' and Grid[2][2] == 'O':
            print("player 2 wins!")
            x = True
        elif Grid[0][0] == 'O' and  Grid[1][1] == 'O' and Grid[2][2] == 'O':
            print("player 2 wins!")
            x = True
        elif Grid[0][2] == 'O' and  Grid[1][1] == 'O' and Grid[0][1] == 'O':
            print("player 2 wins!")
            x = True






    DisplayGrid()
    while x == False:
        YourGo = input ("Your go\n Enter Number -  ")
        #Testing...
        print(x)
        #Testing...
        if YourGo == '7':
            Grid[0][0] = 'X'
            DisplayGrid()
            check()
        elif YourGo == '8':
            Grid[0][1] = 'X'        #Player one's turn
            DisplayGrid()
            check()
        elif YourGo == '9':
            Grid[0][2] = 'X'
            DisplayGrid()
            check()
        elif YourGo == '4':
            Grid[1][0] = 'X'
            DisplayGrid()
            check()
        elif YourGo == '5':
            Grid[1][1] = 'X'
            DisplayGrid()
            check()
        elif YourGo == '6':
            Grid[1][2] = 'X'
            DisplayGrid()
            check()
        elif YourGo == '1':
            Grid[2][0] = 'X'
            DisplayGrid()
            check()
        elif YourGo == '2':
            Grid[2][1] = 'X'
            DisplayGrid()
            check()
        elif YourGo == '3':
            Grid[2][2] = 'X'
            DisplayGrid()
            check()

        YourGo2 = input("Play 2's turn\nEnter number - ")

        if YourGo2 == '7':
            Grid[0][0] = 'O'
            DisplayGrid()
            check()
        elif YourGo2 == '8':            #player Two's turn
            Grid[0][1] = 'O'
            DisplayGrid()
            check()
        elif YourGo2 == '9':
            Grid[0][2] = 'O'
            DisplayGrid()
            check()
        elif YourGo2 == '4':
            Grid[1][0] = 'O'
            DisplayGrid()
            check()
        elif YourGo2 == '5':
            Grid[1][1] = 'O'
            DisplayGrid()
            check()
        elif YourGo2 == '6':
            Grid[1][2] = 'O'
            DisplayGrid()
            check()
        elif YourGo2 == '1':
            Grid[2][0] = 'O'
            DisplayGrid()
            check()
        elif YourGo2 == '2':
            Grid[2][1] = 'O'
            DisplayGrid()
            check()
        elif YourGo2 == '3':
            Grid[2][2] = 'O'
            DisplayGrid()
            check()





def DisplayGrid():
    print("  -----------")
    DisplayRow(Grid[0][0],Grid[0][1],Grid[0][2])
    print("  -----------")
    DisplayRow(Grid[1][0],Grid[1][1],Grid[1][2])
    print("  -----------")
    DisplayRow(Grid[2][0],Grid[2][1],Grid[2][2])
    print("  -----------")

def DisplayRow (a,b,c):
    print(" | ",end="")
    print(a,end="")
    print(" | ",end="")
    print(b,end="")
    print(" | ",end="")
    print(c,end="")
    print(" |")

NAC()

Dani AI

Generated

Good starting point — two practical fixes will get this reliable quickly.

First, there’s a logic bug in your win tests: the anti‑diagonal is checked with the wrong index (Grid[0][2], Grid[1][1], Grid[0][1] in your post). That will never match a real diagonal and will silently prevent some wins from being detected. Second, avoid relying on changing a module/global flag from deep inside a nested function. As suggested, move the winner test out of NAC and make it return a result; returning makes control flow explicit and removes fragile scoping worries.

Here is a compact, easy-to-read winner check you can drop in and call after every move (keeps the winning lines in one place so tests are correct and obvious):

winning_lines = [
  ((0,0),(0,1),(0,2)), ((1,0),(1,1),(1,2)), ((2,0),(2,1),(2,2)),
  ((0,0),(1,0),(2,0)), ((0,1),(1,1),(2,1)), ((0,2),(1,2),(2,2)),
  ((0,0),(1,1),(2,2)), ((0,2),(1,1),(2,0))
]

def check_winner(grid):
    for a,b,c in winning_lines:
        v = grid[a[0]][a[1]]
        if v != ' ' and v == grid[b[0]][b[1]] == grid[c[0]][c[1]]:
            return v   # 'X' or 'O'
    return None

Practical tips: map keypad input to coordinates with a small dict (so you don’t repeat nine if/elif blocks), verify a chosen cell is empty before writing, and break the main loop when check_winner returns a value (print the message there). Rename functions/vars to snake_case and keep display, input/validation, and game logic separate — it makes debugging much easier. This approach avoids globals, fixes the diagonal bug, and makes adding ties/AI or saving state straightforward.

Recommended Answers

All 2 Replies

Take check() out from under NAC()--capture the return from a function

def check(player):
    if player == Grid[0][0] == Grid[0][1] == Grid[0][2]:
        print("%s wins!", (player))
        return True

Please read the Python Style Guide to make your code readable. Functions and variable names are lower case with underscores.

You have redundant code in the if statements

if YourGo == '7':
    Grid[0][0] = 'X'
elif YourGo == '8':
    Grid[0][1] = 'X' #Player one's turn

DisplayGrid()
check()

Also, you can map the squares to the numbers and use a for loop or simpify like this

if YourGo == '7':
    Grid[0][0] = 'X'
elif YourGo == '8':
    Grid[0][1] = 'X'
elif YourGo == '9':
    Grid[0][2] = 'X'

#------------------------------------
# replace with
this_row ['7', '8', '9']:
if YourGo in this_row:
    row = 0
    col = this_row.index(YourGo)

Grid[row][col] = 'X'

Finally, turn the above into a function and send the player to it so you don't test separately for X and O, i.e. the same code twice.

commented: Helped me alot! +0

Thank You!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.