I'm taking a course online to learn python. I am working on a project to code a tic-tac-toe game. I've built this game around a nested list represented by the 3x3 tic-tac-toe grid. Here is the nested list:
grid = [[1,2,3],[4,5,6],[7,8,9]]
I'd like to prompt the user to select a number 1-9 and then replace the selected number in the nested list with either an X or an O depending on which player made the selection.
I can find the value inside the list just fine, and it will print out okay, along with the number that was entered by the user. However, when I try to compare the two values with an if statement, nothing happens. I'd like to just update the nested list value after the if statement but I can't figure out what I'm doing wrong or why the if statement won't fire. I can't get the value to update so I replaced that line with a print statement just to see how it would handle but the line I'm trying to print just gets ignored. Here is the if statement, where p = the number input by the user.
for r in grid:
for c in r:
print str(c) + " / " + str(p) # so I can see the values
if c == p:
print "Winner winner, chicken dinner!"
When I run the code and feed it an integer (in this case 4), I expect to see the chicken dinner line printed out but instead I just get the following:
1 / 4
2 / 4
3 / 4
4 / 4
5 / 4
6 / 4
7 / 4
8 / 4
9 / 4
Why doesn't it recognize that 4 == 4?
UPDATE: I tried sticking the variables in the str() to make sure they were the same type, but I got the same results. Here is the whole code so far:
grid = [['1','2','3'],['4','5','6'],['7','8','9']]
plyr = ("X","O")
turn = 0
def drw_brd():
i = 1
f = turn
for spc in grid:
print " " + spc[0] + " | " + spc[1] + " | " + spc[2] + " "
if i<=2:
print "-----------"
i+=1
print''
print "Player %s (%s's) it's your turn!" %(str(f+1),plyr[turn])
place = input('Cell number to take:')
place_sym(int(place))
check_win()
def check_win():
switch_plyr()
def switch_plyr():
global turn
"""
if turn == 0:
turn = 1
else:
turn = 0
"""
if turn <= 0:
turn = 1
elif turn >= 1:
turn = 0
#print turn
drw_brd()
def place_sym(p):
global turn
global grid
global plyr
print plyr[turn]
for r in grid:
for c in r:
print str(c) + " / " + str(p)
if c == p:
print "Winner winner, chicken dinner!"