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 == 'X' and  Grid == 'X' and Grid == 'X':
print("player 1 wins!")
global x
x = True
#Only Using This for testing
elif Grid == 'X' and  Grid == 'X' and Grid == 'X':
print("player 1 wins!")
x = True
elif Grid == 'X' and  Grid == 'X' and Grid == 'X':
print("player 1 wins!")
x = True
elif Grid == 'X' and  Grid == 'X' and Grid == 'X':
print("player 1 wins!")
x = True
elif Grid == 'X' and  Grid == 'X' and Grid == 'X':
print("player 1 wins!")
x = True
elif Grid == 'X' and  Grid == 'X' and Grid == 'X':
print("player 1 wins!")
x = True
elif Grid == 'X' and  Grid == 'X' and Grid == 'X':
print("player 1 wins!")
x = True
elif Grid == 'X' and  Grid == 'X' and Grid == 'X':
print("player 1 wins!")
x = True

if Grid == 'O' and  Grid == 'O' and Grid == 'O':
print("player 2 wins!")
x = True
elif Grid == 'O' and  Grid == 'O' and Grid == 'O':
print("player 2 wins!")
x= True
elif Grid == 'O' and  Grid == 'O' and Grid == 'O':
print("player 2 wins!")
x = True
elif Grid == 'O' and  Grid == 'O' and Grid == 'O':
print("player 2 wins!")
x = True
elif Grid == 'O' and  Grid == 'O' and Grid == 'O':
print("player 2 wins!")
x = True
elif Grid == 'O' and  Grid == 'O' and Grid == 'O':
print("player 2 wins!")
x = True
elif Grid == 'O' and  Grid == 'O' and Grid == 'O':
print("player 2 wins!")
x = True
elif Grid == 'O' and  Grid == 'O' and Grid == '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 = 'X'
DisplayGrid()
check()
elif YourGo == '8':
Grid = 'X'        #Player one's turn
DisplayGrid()
check()
elif YourGo == '9':
Grid = 'X'
DisplayGrid()
check()
elif YourGo == '4':
Grid = 'X'
DisplayGrid()
check()
elif YourGo == '5':
Grid = 'X'
DisplayGrid()
check()
elif YourGo == '6':
Grid = 'X'
DisplayGrid()
check()
elif YourGo == '1':
Grid = 'X'
DisplayGrid()
check()
elif YourGo == '2':
Grid = 'X'
DisplayGrid()
check()
elif YourGo == '3':
Grid = 'X'
DisplayGrid()
check()

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

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

def DisplayGrid():
print("  -----------")
DisplayRow(Grid,Grid,Grid)
print("  -----------")
DisplayRow(Grid,Grid,Grid)
print("  -----------")
DisplayRow(Grid,Grid,Grid)
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 == Grid == Grid:
print("%s wins!", (player))
return True
``````

## All 2 Replies

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

``````def check(player):
if player == Grid == Grid == Grid:
print("%s wins!", (player))
return True
``````

You have redundant code in the if statements

``````if YourGo == '7':
Grid = 'X'
elif YourGo == '8':
Grid = '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 = 'X'
elif YourGo == '8':
Grid = 'X'
elif YourGo == '9':
Grid = '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 learning and sharing knowledge.