Question: import random grid _ size = 1 0 grid = [ [ ' ' ] * grid _ size for i in range ( grid

import random
grid_size =10
grid =[['']* grid_size for i in range (grid_size)]
num_of_ships =5
def drawboard(board):
print(""+"".join(str(i) for i in range(grid_size)))
for idx, row in enumerate(board):
print(f"{idx}|"+"".join(row))
def setupboard():
board =[['|' for _ in range(grid_size)] for _ in range(grid_size)]
ships_placed =0
while ships_placed num_of_ships:
row = random.randint(0, grid_size -1)
col = random.randint(0, grid_size -1)
if board[row][col]=='|':
board[row][col]='S'
ships_placed +=1
return board
def checkhitormiss(board, guess_row, guess_col):
if board[guess_row][guess_col]=='S':
board[guess_row][guess_col]='X' # Mark hit with 'X'
return True
elif board[guess_row][guess_col]=='~':
board[guess_row][guess_col]='O' # Mark miss with 'O'
return False
else:
return None # Already guessed position
def isgameover(board):
for row in board:
if 'S' in row:
return False
return True
def main():
player_board = setupboard()
computer_board = setupboard()
while True:
drawboard(player_board)
try:
guess_row = int(input("Guess Row (0-9): "))
guess_col = int(input("Guess Column (0-9): "))
hit = checkhitormiss(computer_board, guess_row, guess_col)
if hit is True:
print("Hit!")
elif hit is False:
print("Miss!")
else:
print("You already guessed that!")
if isgameover(computer_board):
print("Congratulations! You've sunk all my battleships!")
break
except ValueError:
print("Please enter valid numbers.")
except IndexError:
print("Oops! That's out of bounds. Try again.")
if __name__=="__main__":
main()
I am missing the + and - signs for the background. Can you help me find out what is missing from my code. I need it to look like the picture.
This is how your program should behave after it starts.
import random grid _ size = 1 0 grid = [ [ ' ' ]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!