The following are links to additional questions that our community has asked about this exercise:
This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
Currently there have not been enough questions asked and answered about this exercise to populate this FAQ section.
This FAQ is built and maintained by you, the Codecademy community – help yourself and other learners like you by contributing!
Not seeing your question? It may still have been asked before – try () in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post ().
I have now nested a conditional under the if statement for outside of range. If the coordinates are incorrect 0-4 row and col, it runs the message “You missed…” but any input for col/row 5 and above, an error is printed saying that list index is out of range
HOW COME?!
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)
if guess_row == ship_row and guess_col == ship_col:
print “Congratulations! You sank my battleship!”
else:
if guess_row not in range(0,5) or guess_col not in range(0,5):
print “Oops, that’s not even in the ocean.”
else:
print “You missed my battleship!”
board[guess_row][guess_col] = “X”
print_board(board)
Please show us how your code is indented by using the code icon, </>, found near the middle of the menu bar that appears at the top of the text box when you edit or type.
Did you try running the code? It should have raised an exception on the line I pointed out.
a = 42
if a >= range(42):
print ('Error')
Traceback (most recent call last):
File "main.py", line 34, in <module>
if a >= range(42):
TypeError: '>=' not supported between instances of 'int' and 'range'
My code is encountering an error on first raw_input step:
I’ve tried commenting out the raw_import lines and just directly assigning a variety of integers for the variables guess_row and guess_col and the subsequent logic executes exactly as expected for all if/else conditions.
When I run the code below, it does get to the raw_input step for the variable guess_row and it does prompt the user for input, but when I provide a value and hit return, nothing happens for a while and then the error is thrown.
Code follows:
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
# I need to figure out why the following line 28
# throws an error after user input is provided
guess_row = int(raw_input("Guess Row: "))
# The error is as follows:
# Traceback (most recent call last):
# File "python", line 27, in <module>
# ExecTimeoutException: Program took too long to terminate.
guess_col = int(raw_input("Guess Col: "))
# alternate approach, directly assigning integer values
# the if/else logic works as expected
# across a variety of value pairs
# guess_row = 6
# guess_col = 4
# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
else:
if guess_row not in range(5) or guess_col not in range(5):
print "Oops, that's not even in the ocean."
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
print_board(board)
Note there is a row # mismatch b/t my comment and what the error shows, that’s b/c I made an edit. When I run this, the error is in fact occurring at the row with the guess_row = int(raw_input("Guess Row: "))
I was able to get it to work after quitting and restarting the browser. Problem does reoccur after a while not sure if it’s the specific browser or whether it’s a more general problem with the functionality running inside a browser. Anyway did get it to run successfully. Thanks!
I made some changes to the code and it worked perfectly.
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"
board[guess_row][guess_col] = "X"
else:
if guess_row not in range(5) or guess_col not in range(5):
print "Oops, that's not even in the ocean."
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
print_board(board)
So if I enter a row or col that is out of range, it prints “Oops, that’s not even in the ocean.”to the console but it’s marked wrong on the exercise and the box int the editor pops up and says: Make sure you print “Oops, that’s not even in the ocean.” if the user guesses a row or column that is off the board. Any suggestions?