FAQ: Learn Python - Battleship - To Your Battle Stations!

This community-built FAQ covers the “To Your Battle Stations!” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise To Your Battle Stations!:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources about Python in general? Go here!

Want to take the conversation in a totally different direction? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account, billing, Pro, or Pro Intensive? Reach out to our support team!

None of the above? Find out where to ask other questions here!

2 posts were split to a new topic: range() and index() Count Differently

How to handle invalid input?
I accidentally hit Return without entering a number, which gave me the idea to check if player input for Battleship was a valid number.
But it seems I can’t check if the input was a number:
raw_input will always be a string first.
But as soon as I call int() on something that is not a number-string, I get an “invalid literal for int()” error.
I want to ask “Is the input an integer?” without running into that error.

I would be interested to learn how I can do this check!
Thanks a lot.

1 Like

How could I modify my code to stop printing the board again in case of “Game Over”, just like it doesn’t print it again in case of a win?

1 Like

Hey!
I completed the battleship section, but when I put it into a python2 ide on my phone to show a friend, it showed an error about ascii or something. Why does it not work in the ide? please help, thanks for listening.

Please share your code and the error, otherwise its near impossible to tell

Hi,
I was wondering how, when the player guesses a number out the range, not to use up their go, but to stay on the turn they were on.
Eg. if a player is on turn 2 and guesses row 10, it returns “Oops, that’s not even in the ocean.” and then their next go is still turn 2.

I have tried using turn = turn - 1, after this if statement, but it doesnt seem to do anything :frowning:

any help would be great.
Thanks

this would make the amount of turns unknown, thus you need a while loop

ahh, ok
I converted my code to this:

turn = 0
while turn < 4:
  print "Turn", turn + 1
  guess_row = int(raw_input("Guess Row: "))
  guess_col = int(raw_input("Guess Col: "))

  if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sunk my battleship!"
    break
  else:
    if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
      print "Oops, that's not even in the ocean."
    elif(board[guess_row][guess_col] == "X"):
      print "You guessed that one already."
    else:
      print "You missed my battleship!"
      board[guess_row][guess_col] = "X"
      if turn == 3:
        sleep(1)
        print "Game Over"
      turn = turn + 1
  # Print (turn + 1) here!
  print_board(board)

but its not running again for some reason :confused:
I am not sure if its my programs error or something to do with my computer or something. I cant seem to see anything wrong, but obviously there could be. Do you think you could have aquick glance over it?

can you post your full code? So i can be very sure that i don’t run into any unexpected issues

sure :slight_smile:

from random import randint
from time import sleep

board = []

for x in range(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)

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
turn = 0
while turn < 4:
  print "Turn", turn + 1
  guess_row = int(raw_input("Guess Row: "))
  guess_col = int(raw_input("Guess Col: "))

  if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sunk my battleship!"
    break
  else:
    if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
      print "Oops, that's not even in the ocean."
    elif(board[guess_row][guess_col] == "X"):
      print "You guessed that one already."
    else:
      print "You missed my battleship!"
      board[guess_row][guess_col] = "X"
      if turn == 3:
        sleep(1)
        print "Game Over"
      turn = turn + 1
  # Print (turn + 1) here!
  print_board(board)

i ran your cod here:

https://repl.it/@stetim94/SandybrownDeepskyblueTahr

i can’t find any very obviously flaws, everything seems to work fine:

tested everything (misses, same guess), and you stay at the same turn

1 Like

Hi, when i execute the program (“RUN”) and if i take a little too much time to entered my guess, i have the following message : “ExecTimeoutException: Program took too long to terminate.”
How can i extend this time to have a little bit more time to respond ?
Eric

Nobody else seems to have this problem as I can see but whenever I run my code it never lets my place a guess for the guess_col , it automatically uses the guess in guess_row for both the column and row which means that it is near impossible to win even with the exact location being printed. I have followed the tutorial and even let code academy solve a part just to see if it would somehow fix it but it doesn’t. I’m not sure what to do so any help would be greatly appreciated.

Can we see your code?

When ever i put my code in for some reason it still shows where the ship area is. I have no clue why-

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

for turn in range(4):
  print "Turn", turn + 1
  guess_row = int(raw_input("Guess Row: "))
  guess_col = int(raw_input("Guess Col: "))

  if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
    break
  else:
    if guess_row not in range(5) or \
      guess_col not in range(5):
      print "Oops, not on here. (The answer is below!)"
    elif board[guess_row][guess_col] == "X":
      print( "You guessed that one already." )
    else:
      print "You missed my battleship! Try again!"
      board[guess_row][guess_col] = "X"
    if (turn == 3):
      print "Game Over, you lost!"
    print_board(board)

You print the ship coordinates after generating the random ship coordinates (random row and random col)?

I had the same problem, here is my code:

image

image

There is one more image, but it isn’t allowing me to post it.

please copy paste your code to the forum, working from screenshots is really difficult.