I have this problem while making a simple battleship game. Here is my code:
board = []
row = ['O'] * 5 #<<<<determine the board size here
joined_O = ' '.join(row)
for i in range(5): #<<<<determine the board size here
board.append(joined_O)
print(joined_O)
from random import randint #<<<< this code is to determine where the ship is. It is placed randomly.
ship_row = randint(1,len(board))
ship_col = randint(1,len(board))
print(ship_row,', ',ship_col,'\n')
print('Shoot missile to the ship')
missile_row = int(input('row : '))
missile_col = int(input('column: '))
#I really don't know where you're supposed to put the int() thingy so i put it everywhere
if int(missile_row) == int(ship_row) and int(missile_col) == int(ship_col):
print("Congratulation! You've hit the ship.")
break
elif int(missile_row) >= len(board) or int(missile_col) >= len(board):
print('Sorry! Area is out of range.')
break
else:
print('Missile missed the target')
board[int(missile_row)][int(missile_col)] = 'X'
print(board)
I tried to reassign the 'O's where the missile hit with an 'X' but then it says
TypeError: 'str' object does not support item assignment.