0

The goal of my Python program is printing out a user-specified N^N board surrounded with +'s using a 2D array. To do that I first created a 2D list with N+2 lines and rows of +'s. Then using a nested for loop, I tried to replace the +'s with " " on indices 1 to N+1(not including) on each row except the first and the last rows, so that my code would print out a N^N board on the inside, surrounded with +'s. However I could not get my program to skip the first and last lines when erasing the +'s, despite excluding their indices in the 2D List in the nested for loop.

Thank you for your help in beforehand!

Here is my code.

This is the desired output (for N=5)

And this is the output of my code.

edt
  • 3
  • 3
  • Welcome to stack overflow. Try posting code snippets instead of images of your code next time, this helps others help you faster as they don't have to type the code out from the picture all over again :) – Ayush Nov 15 '21 at 12:22

3 Answers3

0

In your code, you do:

board =[['+'] * (N+2)] * (N+2)

This makes a list with N+2 references to the same list.
So everytime you change a 'middle square' to a white space inside your code(board[x][y] = " ") for a particular row x, you're infact actually changing all the tiles in the column y in your board to the white space simultaneously, since all the rows refer to but one list.

Instead, you should use something like this which actually creates (N+2) separate lists:

board =[['+'] * (N+2) for _ in range(N+2)]

This has also been explained in greater detail in this stackoverflow answer

Making the above change in the code results in the output you desire.

Ayush
  • 1,510
  • 11
  • 27
  • Thank you for the super quick response and solution! I'll also check the answer you shared to understand it better. – edt Nov 15 '21 at 12:25
0

Although, it is a good practice to think of your own approaches & code it out when you're learning, but reading other people's code along with that will give you some hints towards multiple better approaches that you should eventually try to come up with. Here, are my two cents. Keeping it simple!

# board dimension
N = 5

# print board
def print_board(board):
    for i in range(N):
        for j in range(N):
            print(board[i][j], end='')
        print()

# declare & populate the board    
board = []
for i in range(N):
    row = []
    for j in range(N):
        row.append('+')
    board.append(row)

# state of board initially
print_board(board)

print()

# Your logic of removing the plus sign, simply don't include the first & last 
# row and column for converting into space
for i in range(1,N-1):
    for j in range(1,N-1):
        board[i][j] = ' '
print_board(board)    
xvzf
  • 137
  • 1
  • 10
0

to make this pattern you can simply use for loops.

N = int(input())

for i in range(N+2):
    for j in range(N+2):
        if(i == 0 or i == N+1 or j == 0 or j == N+1):
            print('+', end = '')
        else:
            print(' ', end = '')
    print()