Reputation: 13
I'm trying to create a board/grid on python 3.5 that looks like this:
The height/width are user inputs.
This is what I currently have:
board = [['*' for i, row in enumerate(range(2))] for row in enumerate(range(3))]
for i, row in enumerate(board):
actual_board = ' '.join(row)
row_text = '{0} '.format(i)
print (row_text + actual_board)
This outputs:
0 * *
1 * *
2 * *
Upvotes: 0
Views: 3872
Reputation: 21609
Another approach, slightly more verbose code.
def makeboard(rows, cols):
board = []
for r in range(rows):
brow = []
for c in range(cols):
if r == c == 0:
brow.append(' ')
elif r == 0:
brow.append(str(c-1))
elif c == 0:
brow.append(str(r-1))
else:
brow.append('*')
board.append(brow)
return board
b = makeboard(4,3)
for row in b:
print ' '.join(row)
Upvotes: 0
Reputation: 49318
When you create the board
, you're not using i
or row
- there's no need to enumerate()
a range()
. Just use range()
.
board = [['*' for column in range(2)] for row in range(3)]
print(' ' + ''.join(map(str, range(2)))) # print column labels
for row, item in enumerate(board): # for each row
print(str(row) + ''.join(item)) # print the row label and contents
Result:
01
0**
1**
2**
Upvotes: 1