Battleship input prompt problem

In page 11/19 in Battleship:

https://www.codecademy.com/courses/learn-python/lessons/battleship/exercises/danger-will-robinson?action=resume_content_item

My code:

from random import randint

board = []

for x in range(0, 5):
  board.append(["O"] * 5)

def print_board(board):
  for row in board:
    print " ".join(row)

print_board(board)

def random_row(board):
  return randint(0, len(board) - 1)

def random_col(board):
  return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)
print ship_row
print ship_col

guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
  print "Congratulations! You sank my battleship!"
else:
  print "You missed by battleship!"
  guess_row = "X"
  guess_col = "X"
  print_board(board)

and the results on Codecademy:

O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
4
4
Guess Row: 4
Guess Col: Congratulations! You sank my battleship!

or if different row/col numbers:

O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
4
1
Guess Row: 3
Guess Col: You missed by battleship!
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O

What am I missing here?
Thanks.

1 Like

Those are temporary variables that will be overwritten on the next guess. What we need to do is modify the board at those coordinates.

1 Like

ah right, thanks @mtf - changed to board[guess_row][guess_col] = “X”

When the board returns, the guesses are all shifted down and to the right (because guesses don’t start at 0,0. Suggestion on how to adjust?

Guess Row: 1
Guess Col: 1
You missed by battleship!
O O O O O
O **X** O O O
O O O O O
O O O O O
O O O O O
2 Likes

The marked element is correct. If you don’t like [0…4] as inputs, then adjust your logic. You will not be able to change how list objects work, so bear that in mind.

1 Like