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

faq

#1

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!


split this topic #2

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


#3

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.


#4

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?


#5

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.


#6

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


#7

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


#8

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


#9

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?


#10

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


#11

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)

#12

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