How can I make sure my function properly updates my dictionary key value pair?
Question:
I have read through a few other posts and did not find what I was looking for. I am new to python and have just reached my first milestone project in my course which is to build a Tic Tac Toe game. I am using google as a reference, but ultimately trying to figure out the challenge without looking at the solution.
-
I have a dictionary that stores the value of each position on the board. a1 = top left, c3 = bottom right, etc
-
I have a function print_board() that re prints the tic tac toe board.
-
I have a function called reassign() that takes an input (dictionary key) and changes the value associated with that key. <—– This part is not working, and I’m not sure what my problem is, or what specifically I should be reading to find the answer.
So far the players are asked for inputs and the board prints correctly, but when I enter the board position (a1 for example), the dictionary does not update with the new value. When I enter a1, rather than a1 = "x", a1 is still " ".
Thanks in advance – code is below.
#Board Positions
boardPos = {
"a1" : " ",
"a2" : " ",
"a3" : " ",
"b1" : " ",
"b2" : " ",
"b3" : " ",
"c1" : " ",
"c2" : " ",
"c3" : " "
}
#winning combinations
#The Board
colLab = " 1 2 3"
row1 = "A" + boardPos.get("a1") + " |" + boardPos.get("a2") + "|" + boardPos.get("a3")
row2 = "B" + boardPos.get("b1") + " |" + boardPos.get("b2") + "|" + boardPos.get("b3")
row3 = "C" + boardPos.get("c1") + " |" + boardPos.get("c2") + "|" + boardPos.get("c3")
#Function for printing the current game board
def print_board() :
print(colLab)
print("x1B[4m" + row1 + "x1b[0m")
print("x1B[4m" + row2 + "x1b[0m")
print(row3)
def reassign(pos, val) :
boardPos[pos] == val
print(boardPos[pos])
print("Welcome to Tic-Tac-Toe nThis game requires two players. nThe rules are simple. One player is X and the other O. nTo win, position your X or O three in a row either horizontally, vertically or diagonally.")
print("To start, reference the A B C 1 2 3 on the border of the board to make your first move. nFor example: if you want to place your character in the top left corner, you will type A1.")
print_board()
i = 1 # number of turns
lastTurn = ""
while i < 9 :
print("It is player 1's Turn")
reassign(input(), "x")
i += 1
print_board()
print("It is player 2's Turn")
reassign(input(), "o")
i += 1
print_board()
Answers:
You are using == to assign new value. It is used to compare.
Change your code to
boardPos[pos] = val
There are two problems here:
boardPos[pos] == val
This is an equality test: it will return True
or False
. Change the ==
to =
.
Additionally:
row1 = "A" + boardPos.get("a1") + " |" + boardPos.get("a2") + "|" + boardPos.get("a3")
row2 = "B" + boardPos.get("b1") + " |" + boardPos.get("b2") + "|" + boardPos.get("b3")
row3 = "C" + boardPos.get("c1") + " |" + boardPos.get("c2") + "|" + boardPos.get("c3")
These three lines are defined once and never updated, so when you call print_board()
you will not see the correct result. You can resolve this by editing the function as follows:
def print_board() :
colLab = " 1 2 3"
row1 = "A" + boardPos.get("a1") + " |" + boardPos.get("a2") + "|" + boardPos.get("a3")
row2 = "B" + boardPos.get("b1") + " |" + boardPos.get("b2") + "|" + boardPos.get("b3")
row3 = "C" + boardPos.get("c1") + " |" + boardPos.get("c2") + "|" + boardPos.get("c3")
print(colLab)
print("x1B[4m" + row1 + "x1b[0m")
print("x1B[4m" + row2 + "x1b[0m")
print(row3)
Once all of your code works properly, you can also seek general improvements on code review.
I have read through a few other posts and did not find what I was looking for. I am new to python and have just reached my first milestone project in my course which is to build a Tic Tac Toe game. I am using google as a reference, but ultimately trying to figure out the challenge without looking at the solution.
-
I have a dictionary that stores the value of each position on the board. a1 = top left, c3 = bottom right, etc
-
I have a function print_board() that re prints the tic tac toe board.
-
I have a function called reassign() that takes an input (dictionary key) and changes the value associated with that key. <—– This part is not working, and I’m not sure what my problem is, or what specifically I should be reading to find the answer.
So far the players are asked for inputs and the board prints correctly, but when I enter the board position (a1 for example), the dictionary does not update with the new value. When I enter a1, rather than a1 = "x", a1 is still " ".
Thanks in advance – code is below.
#Board Positions
boardPos = {
"a1" : " ",
"a2" : " ",
"a3" : " ",
"b1" : " ",
"b2" : " ",
"b3" : " ",
"c1" : " ",
"c2" : " ",
"c3" : " "
}
#winning combinations
#The Board
colLab = " 1 2 3"
row1 = "A" + boardPos.get("a1") + " |" + boardPos.get("a2") + "|" + boardPos.get("a3")
row2 = "B" + boardPos.get("b1") + " |" + boardPos.get("b2") + "|" + boardPos.get("b3")
row3 = "C" + boardPos.get("c1") + " |" + boardPos.get("c2") + "|" + boardPos.get("c3")
#Function for printing the current game board
def print_board() :
print(colLab)
print("x1B[4m" + row1 + "x1b[0m")
print("x1B[4m" + row2 + "x1b[0m")
print(row3)
def reassign(pos, val) :
boardPos[pos] == val
print(boardPos[pos])
print("Welcome to Tic-Tac-Toe nThis game requires two players. nThe rules are simple. One player is X and the other O. nTo win, position your X or O three in a row either horizontally, vertically or diagonally.")
print("To start, reference the A B C 1 2 3 on the border of the board to make your first move. nFor example: if you want to place your character in the top left corner, you will type A1.")
print_board()
i = 1 # number of turns
lastTurn = ""
while i < 9 :
print("It is player 1's Turn")
reassign(input(), "x")
i += 1
print_board()
print("It is player 2's Turn")
reassign(input(), "o")
i += 1
print_board()
You are using == to assign new value. It is used to compare.
Change your code to
boardPos[pos] = val
There are two problems here:
boardPos[pos] == val
This is an equality test: it will return True
or False
. Change the ==
to =
.
Additionally:
row1 = "A" + boardPos.get("a1") + " |" + boardPos.get("a2") + "|" + boardPos.get("a3")
row2 = "B" + boardPos.get("b1") + " |" + boardPos.get("b2") + "|" + boardPos.get("b3")
row3 = "C" + boardPos.get("c1") + " |" + boardPos.get("c2") + "|" + boardPos.get("c3")
These three lines are defined once and never updated, so when you call print_board()
you will not see the correct result. You can resolve this by editing the function as follows:
def print_board() :
colLab = " 1 2 3"
row1 = "A" + boardPos.get("a1") + " |" + boardPos.get("a2") + "|" + boardPos.get("a3")
row2 = "B" + boardPos.get("b1") + " |" + boardPos.get("b2") + "|" + boardPos.get("b3")
row3 = "C" + boardPos.get("c1") + " |" + boardPos.get("c2") + "|" + boardPos.get("c3")
print(colLab)
print("x1B[4m" + row1 + "x1b[0m")
print("x1B[4m" + row2 + "x1b[0m")
print(row3)
Once all of your code works properly, you can also seek general improvements on code review.