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


#1

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
#2

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

#3

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


#4
    [str(x)] + ['O'] * 5
         ^
      number

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']

#5

You’re a genious, thankss :heart:


#6

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