I have a dictionary that I want to use to change a matrix so I don't have to program a bunch of if statements by hand. However, the program is not recognizing it.
player_board = [["", "A", "B", "C", "D", "E", "F", "G", "H"],
["1", " ", " ", " ", " ", " ", " ", " ", " "],
["2", " ", " ", " ", " ", " ", " ", " ", " "],
["3", " ", " ", " ", " ", " ", " ", " ", " "],
["4", " ", " ", " ", " ", " ", " ", " ", " "],
["5", " ", " ", " ", " ", " ", " ", " ", " "],
["6", " ", " ", " ", " ", " ", " ", " ", " "],
["7", " ", " ", " ", " ", " ", " ", " ", " "],
["8", " ", " ", " ", " ", " ", " ", " ", " "]]
screen_encrypt = {
"A1": player_board[1][1],
"A2": player_board[2][1],
"A3": player_board[3][1],
"A4": player_board[4][1],
"A5": player_board[5][1],
"A6": player_board[6][1],
"A7": player_board[7][1],
"A8": player_board[8][1],
"B1": player_board[1][2],
"B2": player_board[2][2],
"B3": player_board[3][2],
"B4": player_board[4][2],
"B5": player_board[5][2],
"B6": player_board[6][2],
"B7": player_board[7][2],
"B8": player_board[8][2],
}
inp = "A1"
screen_encrypt[inp] = "X"
for row in player_board:
print(row)
This is the the output:
['', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
['1', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['3', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['4', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['5', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['6', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['7', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['8', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
This is the desired output:
['', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
['1', 'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['3', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['4', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['5', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['6', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['7', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['8', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
CodePudding user response:
The issue here is that you're only changing the screen_encrypt
object, rather than modifying the player_board
object directly. To fix that, you could update your code to directly update the player_board
instead.
For example:
player_board = [["", "A", "B", "C", "D", "E", "F", "G", "H"],
["1", " ", " ", " ", " ", " ", " ", " ", " "],
["2", " ", " ", " ", " ", " ", " ", " ", " "],
["3", " ", " ", " ", " ", " ", " ", " ", " "],
["4", " ", " ", " ", " ", " ", " ", " ", " "],
["5", " ", " ", " ", " ", " ", " ", " ", " "],
["6", " ", " ", " ", " ", " ", " ", " ", " "],
["7", " ", " ", " ", " ", " ", " ", " ", " "],
["8", " ", " ", " ", " ", " ", " ", " ", " "]]
col_header: list = player_board[0]
inp = "A1"
alpha = inp[0]
# convert string -> int
digit = int(inp[1:])
player_board[col_header.index(alpha)][digit] = 'X'
for row in player_board:
print(row)
Output:
['', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
['1', 'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['3', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['4', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['5', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['6', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['7', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['8', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
CodePudding user response:
Strings are called immutable, they cannot be changed. If you use a mutable object,
you can change it. The simplest mutable object is a list
:
player_board = [["", "A", "B", "C", "D", "E", "F", "G", "H"],
["1", [" "], [" "], [" "], [" "], [" "], [" "], [" "], [" "]],
["2", [" "], [" "], [" "], [" "], [" "], [" "], [" "], [" "]],
["3", [" "], [" "], [" "], [" "], [" "], [" "], [" "], [" "]],
["4", [" "], [" "], [" "], [" "], [" "], [" "], [" "], [" "]],
["5", [" "], [" "], [" "], [" "], [" "], [" "], [" "], [" "]],
["6", [" "], [" "], [" "], [" "], [" "], [" "], [" "], [" "]],
["7", [" "], [" "], [" "], [" "], [" "], [" "], [" "], [" "]],
["8", [" "], [" "], [" "], [" "], [" "], [" "], [" "], [" "]]
You can then say
screen_encrypt[inp][0] = "X"
Which will do what you want.
If you want to get more pythonic, you could create a Cell
object:
class Cell:
def __init__(self):
self.value = " "
def __str__(self):
return self.value
player_board = [["", "A", "B", "C", "D", "E", "F", "G", "H"],
["1", Cell(), Cell(), Cell(), Cell(), Cell(), Cell(), Cell(), Cell()],
["2", Cell(), Cell(), Cell(), Cell(), Cell(), Cell(), Cell(), Cell()],
["3", Cell(), Cell(), Cell(), Cell(), Cell(), Cell(), Cell(), Cell()],
#... etc
You'd then say
screen_encrypt[inp].value = "X"
You'd have to modify your display code to call str()
on each cell to turn it into a nice format:
for row in player_board:
for cell in row:
print(str(cell), end=" ")
print()
CodePudding user response:
You can't use a dictionary but you could use a function like this:
player_board = [["", "A", "B", "C", "D", "E", "F", "G", "H"],
["1", " ", " ", " ", " ", " ", " ", " ", " "],
["2", " ", " ", " ", " ", " ", " ", " ", " "],
["3", " ", " ", " ", " ", " ", " ", " ", " "],
["4", " ", " ", " ", " ", " ", " ", " ", " "],
["5", " ", " ", " ", " ", " ", " ", " ", " "],
["6", " ", " ", " ", " ", " ", " ", " ", " "],
["7", " ", " ", " ", " ", " ", " ", " ", " "],
["8", " ", " ", " ", " ", " ", " ", " ", " "]]
def boardUpdate(position, value, board):
colPositions = {"A":1,"B":2,"C":3,"D":4,"E":5,"F":6,"G":7, "H":8}
index1 = colPositions[position[0]]
index2 = position[1]
board[int(index1)][int(index2)] = value
return board
result = boardUpdate("A1", "X", player_board)
for row in result:
print(row)
Output:
['', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
['1', 'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['2', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['3', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['4', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['5', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['6', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['7', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
['8', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']