About matrix/boards (6)


#1


I want to make a board/matrix the size I want and with what I want in.
It's for the most part, for my curiosity, I was wondering


matrix = []

n = int(input("Enter the matrix/board dimension : "))
t = 1

for x in range(0,n):
	r=['O']*n
	matrix.append(r)
for row in matrix:
	print (" ".join(row))
        
for i in range(n):
	for j in range(n):
		t += 1
		x = int(input("Enter the wanted value : "))

print(matrix)

OUTPUT:

Enter the matrix/board dimension : 3
O O O
O O O
O O O
Enter the wanted value : 1
Enter the wanted value : 0
Enter the wanted value : 0
Enter the wanted value : 0
Enter the wanted value : 1
Enter the wanted value : 0
Enter the wanted value : 0
Enter the wanted value : 0
Enter the wanted value : 1

[['O', 'O', 'O'], ['O', 'O', 'O'], ['O', 'O', 'O']]

Before, I also got an error like I wasn't able to join elements that aren't iterable.


#2

Hi, @betaninja08471 ,

Perhaps write a function that initializes and returns a board according to specifications provided as function arguments. Then anyone using the function can add pertinent code to modify individual cells, for example, as you have already done.

Here's an example of a function that creates the board, accompanied by the function from Battleship that displays the board ...

def board_maker(nrows, ncols, content):
    # Create a board ,nrows by ncols, with each cell containing content
    board = []
    for i in range(0, nrows):
        board.append([content] * ncols)
    return board

def print_board(board):
    # Display the board
    for row in board:
        print(" ".join(row))

b = board_maker(6, 8, "#")
print_board(b)

Output ...

# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #
# # # # # # # #

#3

Thanks really much for your answer.
And if I want to edit the content of each cells independently like

1 0 0 or 3 7 85
0 1 0 | 1 18 6
0 0 1 | 0 -2 128

How can I do that ?


#4

Would you want to edit the content of one cell at a time, or edit the whole board at once?

If you want to edit the whole board at once, you could write a function that accepts a two-dimensional list and assigns the items in the list to the appropriate cells. If you would like the user to choose values for each of the cells of the board, you could create a nested loop that prompts the user with each row and column number, asks for a value, then assigns it to the cell.


#5

Actually I've tried but failed miserably trying to code the nested loop


#6

Below is some example code with a nested loop. It was written for Python 3.x. It assumes that the user will be entering valid integers. Feel free to experiment with it. If you need to use it for Python 2.x, change the input function calls to raw_input function calls.

# create_board.py
def create_board():
    nrows = int(input("How many rows? "))
    ncols = int(input("How many cols? "))
    # Initialize all cells to 0, using a nested list comprehension
    board = [[0 for col in range(ncols)] for row in range(nrows)] 
    # Prompt user for numbers
    for row in range(nrows):
        for col in range(ncols):
            board[row][col] = int(input("row {:d} col {:d}: ".format(row, col)))
    return board

def print_board(board):
    # Display the board
    print(board)                               

b = create_board()
print_board(b)

#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.