# FAQ: Learn Python - Battleship - Bad Aim

This community-built FAQ covers the “Bad Aim” exercise in Codecademy’s lessons on Python.

## Join the Discussion. We Want to Hear From You!

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

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

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!

## Other FAQs

• This list will contain other frequently asked questions that aren’t quite as popular as the ones above.
• 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 ().

A post was split to a new topic: Why is print the board outside of the nested else

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)

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: "))

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.

``````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)

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

print ship_row
print ship_col

if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
elif guess_row >= range(len(board)) or guess_col >= range(len(board[0])):
print "Oops, that's not even in the ocean."
else:
print "You missed my battleship!"
board[guess_row][guess_col]="X"
``````

Any reason that wouldn’t work? specifically the elif.

Doesn’t work, or is not accepted?

One possible problem is comparing a number to a range. They are incompatible types so this should raise an exception.

Instead, use the member test operator, `in` with a logical `not`

Eg.

``if a not in range(len(b))``

It is not accepted by the checker, no errors were posted.

Not accepted because it does not follow the instruction: They don’t want an elif, but rather

Add a new `if` statement that is nested under the `else` .

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

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)
``````
1 Like

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: "))

Were you able to solve this problem?

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!

1 Like

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: "))