Hi everyone, I'm having trouble creating a program. This program needs to create a square board of size n, where a user inputs an integer value 2 - 9. For example, a board with size n=4 would look like:
1 . . .
2 . . .
3 . . .
. a b c
and n = 6 would look like:
1 . . . . .
2 . . . . .
3 . . . . .
4 . . . . .
5 . . . . .
. a b c d e

I'm really stuck and any help would be greatly appreciated, cheers!

Post your code and we will check it out. It is not very hard if you keep your head straight! If you have learned about dictionaries, simplest to operate with is to index dictionary like: board or board and get user input like:
b2, 4

Hi tonyjv, thanks for your reply. We have not yet covered dictionaries. It is suggested to make a list of strings so n = 4 would give board = which would produce
1 . . .
2 . . .
3 . . .
. a b c
But yeah, I have no idea how to write the code for this!
Thanks for taking an interest and thanks for the help!

I thought your task was to input values to board and show the board situation. I need better explanation and effort to start from you. If not in code then in pseudo code (verbaly)

Ok so basically I want to create a function(s) that will print and store a 'board'. This is the start of a much larger project. To begin, the user is prompted to enter the desired board size by typing in an integer 2-9. The purpose of the first function I want, is to initialise a board of size n. This should be enough to get me started.
Sorry for not even posting psuedo code, but I really have no idea how to get started.
Cheers again

This gives maybe some idea, how I would do it. Do first simple version with simple printing, fixed input. Add interactivity and formatting later, but make provisions for it ready.

``````def make_board(n):
return [['.' for count in range(n)] for rows in range(n)]

def print_board(board):
# todo: coordinates
for row in board:
print(' '.join(row))
print('')

def get_board_size():
# todo: user input
return 6

def test():
n = get_board_size()
myboard = make_board(n)
print_board(myboard)
# put value as string, indexes are 0-based
myboard[1][2] = str(6)
print_board(myboard)

if __name__ == '__main__':
test()``````
``````n=6
print "\n".join([str(i+1)+'.'*n for i in range(n)]+["".join(chr(i) if i>=97 else '.' for i in range(96,97+n-1))])``````

Nice code golf, but I think more newbie friendly way exist with simple changes (involving enumerate with start index 1) to my code lines 6 to 8 and also your code put hard coded '.' values in the grid instead of board values. For line 8 you could utilize slicing of string.ascii_lowercase instead of chr stuff. (yes I did code it, output of n=9 below)

``````1: . . . . . . . . .
2: . . . . . . . . .
3: . . . . . . . . .
4: . . . . . . . . .
5: . . . . . . . . .
6: . . . . . . . . .
7: . . . . . . . . .
8: . . . . . . . . .
9: . . . . . . . . .
a b c d e f g h i

1: . . . . . . . . .
2: . . 6 . . . . . .
3: . . . . . . . . .
4: . . . . . . . . .
5: . . . . . . . . .
6: . . . . . . . . .
7: . . . . . . . . .
8: . . . . . . . . .
9: . . . . . . . . .
a b c d e f g h i``````

You are right tonyjv. My code does exactly what is asked for in the first post. No more, no less.

So if it is not sufficient, which is probably the case, then the question was not correct enough.

So in the other post, if I have n and want to make board =
Then my code solves this like:
[str(i+1)+'.'*n for i in range(n)]+["".join(chr(i) if i>=97 else '.' for i in range(96,97+n-1))]

But this is not the real question, because the real question is probably:
I have an n, which is a user input number.
I want to make a board that represents an n*n matrix with unknown kind of data.
I want to print out this board initially like (for n=3):
1 . . .
2 . . .
3 . . .
. a b c

I had also opinion that the list suggestion was inappropriate as the board layout is not object of the game and it is planned to become step by step proper program.

I did catch somehow your intention as I know the kind of code you are able to produce and I know that you know how understandable your code was to newbie and you know that.

To do the stupid answer to stupid suggestion I can do also. However I would do even more literal newbie answer than you:

``````board = [
'1.........',
'2.........',
'3.........',
'4.........',
'5.........',
'6.........',
'7.........',
'8.........',
'9.........',
'.abcdefghi']

n = 6
for i in range(n):
print(board[i][:n])
print(board[-1][:n])``````

Hey guys, thanks for your responses.
I don't really understand what tonyjv did with his code above to produce the n = 10 grid, so I will just go with the post directly above this one.
Thanks again.

Hey guys, thanks for your responses.
I don't really understand what tonyjv did with his code above to produce the n = 10 grid, so I will just go with the post directly above this one.
Thanks again.

Just correct the bug I put there, and add user input. That code is not so useful considering later development. Better the function using divide and conquer way.

Tony

I will like to see the code whereby instead of predefining the size of the board, you ask a user to enter the size of the board

commented: Do ask your own questions instead of hijacking 10 year old ones. And do show what you've tried already while you're at it -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.