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()

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.