Search code examples
pythonarraystic-tac-toe

Tic Tac Toe project


I don't know why the elements in m won't change inside the if statements!

m=[[1,2,3],[4,5,6],[7,8,9]]

def board():

for i in m:
    for j in i:
        print(j, end=" ")
    print('\n')

board()

for k in range(0,9):

global m

position= input('choose a position from 1 to 9: ')

if k%2==0:#even,x
    if position==1:
        m[0][0]='x'
    if position==2:
        m[0][1]='x'
    if position==3:
        m[0][2]='x'
elif k%2==1:#odd,o
    if position==1:
        m[0][0]='o'
    if position==2:
        m[0][1]='o'
    if position==3:
        m[0][2]='o'            
board()

Solution

  • In Python 3, input() function returns a string, which first needs to be cast to an integer before numeric comparison.

    Use this line:

    posistion= int(input('choose a position from 1 to 9: '))
    

    Also, no need for global m, you can remove that line and will keep the same behavior.