How do I add letters and numbers for each row and col?


I wanna make the game like a real one
Please help me out, I have no idea where to start.

How to add labels in battleship

Since we manually build the table (grid) we just include that in the construction phase.

header_row = [' ', 'A', 'B', 'C', 'D', 'E']
board  = []
for x in range(5):
    board.append([str(x)] + ['O'] * 5)
board.insert(0, header_row)
for row in board:
  print (' '.join(row))
  A B C D E
0 O O O O O
1 O O O O O
2 O O O O O
3 O O O O O
4 O O O O O


The letters part I understood, but the numbers are still a mistery to me

    [str(x)] + ['O'] * 5

The number must be stored as a string, so we convert it in that operation. That gives us a row…

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


You’re a genious, thankss :heart:


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