Question: How do I get my numbers inside the lines like the photo? My grid needs to look exactly like the photo. This is how your

How do I get my numbers inside the lines like the photo? My grid needs to look exactly like the photo. This is how your program should behave after it starts.
import random
# Constants for the grid
grid_size =10
num_of_ships =5
# Function to draw the board with grid lines
def drawboard(board):
# Print column headers
print(""+"".join(str(i) for i in range(grid_size)))
print("+"+"---+"* grid_size) # Top border
for idx, row in enumerate(board):
# Print each row with borders
row_line ="|".join(row) # Join each cell with "|"
print(f"{idx}|{row_line}|") # Add row header and side borders
print("+"+"---+"* grid_size) # Row separator
# Function to set up the board with ships placed randomly
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
# Function to check if a hit or miss occurred
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
# Function to check if all ships have been hit
def isgameover(board):
for row in board:
if "S" in row:
return False
return True
# Main game function
def main():
# Set up player's and computer's boards
player_board = setupboard()
computer_board = setupboard()
while True:
drawboard(player_board) # Display the player's board
# Get the user's guess
try:
guess_row = int(input("Guess Row (0-9): "))
guess_col = int(input("Guess Column (0-9): "))
if guess_row 0 or guess_row >= grid_size or guess_col 0 or guess_col >= grid_size:
print("Oops! That's out of bounds. Try again.")
continue
# Check if it's a hit or miss on the computer's board
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!")
# Check if the game is over
if isgameover(computer_board):
print("Congratulations! You've sunk all the battleships!")
break
except ValueError:
print("Please enter valid numbers.")
except IndexError:
print("Oops! That's out of bounds. Try again.")
# Run the game
if __name__=="__main__":
main()
How do I get my numbers inside the lines like the

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!