Codecademy Python Answers Battleship

Check: Reading these Books Can Make You An Eminent Hacker 

Battleship answers:In this project you will build a simplified, one-player version of the classic board game Battleship! In this version of the game, there will be a single ship hidden in a random location on a 5×5 grid. The player will have 10 guesses to try to sink the ship.

To build this game we will use our knowledge of lists, conditionals and functions in Python. When you’re ready to get started, click run to continue.

Watch Codecademy Python Battleship Video

Codecademy Python  Battleship Video Link

Battleship 1/19 – Welcome To Battleship

 

Battleship 2/19 – Getting Our Feet Wet

board = []

 

Battleship 3/19 – Make A List

board = []
for i in range(,5):
    s = ["O"] * 5
    board.append(s)
print board

Battleship 4/19 – Check it wise

board = []
for i in range(,5):
    s = ["O"] * 5
    board.append(s)
    print board

Battleship Answers5/19 – Custom Print

board = []
for i in range(,5):
    s = ["O"] * 5
    board.append(s)
def print_board(board):
    for i in board:
        print i
print_board(board)

 

Battleship Answers 6/19 – Printing Pretty

board = []
for i in range(,5):
    s = ["O"] * 5
    board.append(s)
def print_board(board):
    for i in board:
        print " ".join(i)
print_board(board)

 

Battleship 7/19 – Hide….

from random import randint
board = []

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

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

# Add your code below!
def random_row(board):
    return randint(,len(board)-1)
    
def random_col(board):
    return randint(,len(board)-1)
random_row(board)
random_col(board)
    

 

Battleship 8/19 – …..And Seek

from random import randint

board = []

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

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

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

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

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

# Add your code below!
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

 

Battleship 9/19 – It’s Not Changing,It’s Debugging

from random import randint

board = []

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

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

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

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

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

# Add your code below!
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))
print ship_col
print ship_row

Battleship 10/19 – You Win

from random import randint

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(, len(board) - 1)

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

ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

print ship_row
print ship_col

# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"

Battleship 11/19 – Danger,will Robinson

from random import randint

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(, len(board) - 1)

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

ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

print "Random Row: %s" %(ship_row)
print "Random Col: %s" %(ship_col)

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

Battleship 12/19 – Bad Aim

from random import randint

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(, len(board) - 1)

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

ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

print "Random Row: %s" %(ship_row)
print "Random Col: %s" %(ship_col)

# Write your code below!
if guess_col == ship_col and guess_row == ship_row:
    print "Congratulations! You sank my battleship!"
else:
    print "You missed my battleship!"
    board[guess_row][guess_col] = "X"
    print_board(board)
    if guess_row not in range(5) or guess_col not in range(5):
        print "Oops, that's not even in the ocean."
        

Battleship 13/19 – Not Again

from random import randint

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(, len(board) - 1)

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

ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

print "Random Row: %s" %(ship_row)
print "Random Col: %s" %(ship_col)

# Write your code below!
if guess_col == ship_col and guess_row == ship_row:
    print "Congratulations! You sank my battleship!"
else:
    print "You missed my battleship!"
    board[guess_row][guess_col] = "X"
    print_board(board)
    if guess_row not in range(5) or guess_col not in range(5):
        print "Oops, that's not even in the ocean."
    elif guess_row == 'x' and guess_col == 'x':
        print "You guessed that one already."
        

Battleship 14/19 – Test Run

from random import randint

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(, len(board) - 1)

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

ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

print "Random Row: %s" %(ship_row)
print "Random Col: %s" %(ship_col)

# Write your code below!
if guess_col == ship_col and guess_row == ship_row:
    print "Congratulations! You sank my battleship!"
else:
    print "You missed my battleship!"
    board[guess_row][guess_col] = "X"
    print_board(board)
    if guess_row not in range(5) or guess_col not in range(5):
        print "Oops, that's not even in the ocean."
    elif guess_row == 'x' and guess_col == 'x':
        print "You guessed that one already."
        

Battleship 15/19 – Play It,Sam

from random import randint

board = []

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

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

print "Let's play Battleship!"
print_board(board)

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

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

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

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
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!"
else:
    if (guess_row <  or guess_row > 4) or (guess_col <  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"
        for turn in range(4):
            turn = turn + 1
            print turn
    # Print (turn + 1) here!
    
    print_board(board)

Battleship 16/19 – Game Over-Codecademy Python

from random import randint

board = []

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

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

print "Let's play Battleship!"
print_board(board)

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

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

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

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
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!"
else:
    if (guess_row <  or guess_row > 4) or (guess_col <  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"
        for turn in range(4):
            turn = turn + 1
            print turn
        if turn == 3:
           print "Game Over"
    # Print (turn + 1) here!
    
    print_board(board)

Battleship 17/19 – A Real Win

from random import randint

board = []

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

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

print "Let's play Battleship!"
print_board(board)

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

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

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

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
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!"
    
else:
    if (guess_row <  or guess_row > 4) or (guess_col <  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"
        for turn in range(4):
            turn = turn + 1
            print turn
            break
        if turn == 3:
           print "Game Over"
    # Print (turn + 1) here!
    
    print_board(board)

Battleship 18/19 – To Your Battle Stations

from random import randint

board = []

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

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

print "Let's play Battleship!"
print_board(board)

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

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

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

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
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!"
    
else:
    if (guess_row <  or guess_row > 4) or (guess_col <  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"
        for turn in range(4):
            turn = turn + 1
            print turn
            break
        if turn == 3:
           print "Game Over"
    # Print (turn + 1) here!
    
    print_board(board)

Battleship 19/19 – Extra Credit

If You have Any doubts Regarding Battleship Answers plz comment..let me solve for you….

Please Like,Tweet,G+1 below.. to support us. 🙂 🙂

Also Read :   Codecademy Python Answers Introduction to Classes

9 comments

  1. I don’t understand…copying and pasting this code only lets you attempt once. How do I loop back to make another guess?

  2. OMG this has saved my life. Codecademy is my homework and I completely forgot that this was due in. I had 15 minutes to do this entire section and this saved my LIFE. Thank you SOOOOOOO much. 😀

Leave a Reply

Your email address will not be published. Required fields are marked *