This is my code thus far: I need help realizing what the issue is in swapping out the numbers that appear as the "board" with an X or O. It prints out the board, asks for an X input, then prints out the board, updated with the X (or so I thunk) and prints it out again. It just prints out the same board with no X..
def initial_board(board_dimension):
'''Returns an initialized board for the given board_dimensionension.'''
board = []
position = 1
for i in range(board_dimension):
sublist = []
for j in range(board_dimension):
sublist.append(str(position))
position = 1
board.append(sublist)
return board
def print_board(board):
for row in board:
for i in row:
print(i.rjust(5),end=' ')
print()
def player_x_input(board):
position_x = input("X position: ")
for int in board:
if int == position_x:
int = 'X'
return board
def main():
'''Your main function. Do NOT change the given code.'''
board_dimension = int(input("Input board_dimensionension of the board: "))
board = initial_board(board_dimension)
print_board(board)
player_x_input(board)
print_board(board)
if __name__ == "__main__":
main()
CodePudding user response:
def player_x_input(board):
position_x = input("X position: ")
for pos in range(len(board)):
for elem in range(len(board[pos])):
if board[pos][elem] == position_x:
board[pos][elem] = 'X'
return board
This should do the work
CodePudding user response:
In your loop, you assign to int
(do not use this as a variable name as it is the name of a type) but that does not affect the list.
Consider iterating over the indices of your list instead of it's elements in this case.
>>> lst = [0, 0, 0, 0, 0, 0, 0, 0, 0]
>>> for i in range(len(lst)):
... if i == 5:
... lst[i] = 1
...
>>> lst
[0, 0, 0, 0, 0, 1, 0, 0, 0]
>>>