I'm working on a tic-tac-toe game, and run into a problem.
My problem lies in lines 24/25 and 30/31.
I want to take the returned dictionary from the convert_to_yx() function and assign the keys and values as the x and y of the game_grid I have on line 1. I get a TypeError that says the int is not iterable.
I'm guessing it's a rather simple problem with the syntax somewhere or I'm trying to do something impossible. If there is another way I can take 1 value and return 2 values, I'd be pleased to know.
Thanks for any help. I'll be available to answer any questions about my code should they arise.
game_grid = [{0:None, 1:None, 2:None},
{0:None, 1:None, 2:None},
{0:None, 1:None, 2:None}]
reference_grid = [{0:'1', 1:'2', 2:'3'},
{0:'4', 1:'5', 2:'6'},
{0:'7', 1:'8', 2:'9'}]
def display_grid(grid_type=game_grid):
for x in range(3):
for y in range(3):
print grid_type[y][x],
if y in range(2):
print "|",
if x in range(2):
print "\n--+---+--\n",
#takes the user's choice 1-9 and returns y, x values
def convert_to_yx(choice):
convert_table = {1:{0:0}, 2:{0:1}, 3:{0:2},
4:{1:0}, 5:{1:1}, 6:{1:2},
7:{2:0}, 8:{2:1}, 9:{2:2}}
yx = convert_table[choice]
return yx
#accepts cell choice, sends to convert_to_yx() and applies to grid
def choose_cell():
choice = input("\nWhich spot would you like to place your X? >>> ")
for y, x in convert_to_yx(choice):
game_grid[y][x]='X'
display_grid()
if __name__ == "__main__":
display_grid(reference_grid)
choose_cell()