# FAQ: Learn Python - Battleship - Danger, Will Robinson!

This community-built FAQ covers the " Danger, Will Robinson!!" 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!

1 Like

## 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 ().

2 posts were split to a new topic: How do I set the list element

Why is my code giving this error: ValueError: invalid literal for int() with base 10: ‘RUN’ or ValueError: invalid literal for int() with base 10: ‘SCT’ ? what can I do to solve it?

Thanks

Edit: I’ll leave this here if anyone spots a possible cause. But reloading the webpage seems to get it working fine. I’m guessing cache issue.

The tutorial is giving me the next option, but my first guess is automatically duplicated into the second guess option meaning I can’t guess the column, and the guesses are then only possible on a diagonal from 0,0 to 4,4.

I can’t see a cause, any ideas?

``````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:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
print_board(board)
``````
2 Likes

Hello,

I am confuse about modifying my ocean with an “X” after the player guess a wrong spot.

``````if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
else:
print "You missed my battleship!"
board[guess_row][guess_col]= "X"
``````

When I try to do a quick test on my own in Atom to modify a list like that:

``````n = [1, 5, 7]
n[0][1] = 3
print(n)
``````

I get a message error: “int” object does not support item assignment (I did the test in the text editor Atom)
But in this battleship exercice, that’s the way we do it.
I can’t figure out why it is working in this exercise and not in a script editor.

Above, `n` is a one-dimension array. A double subscript requires a two-dimension array, such as the game board in battleship. There are rows (the first subscript) and columns (the second subscript).

Make sense, thank you.

1 Like

This is a few lessons late but I continually am getting this message even though I’ve gone through several lessons since this issues began.

Traceback (most recent call last):
File “python”, line 24, in
ExecTimeoutException: Program took too long to terminate.

The line it’s referring to is:

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

Upon inputing an answer, I get the response above and am never able to get past that. This is with getting the codecademy solution entered, so it has to be valid.

Thank you.

Hello @djo25 and welcome to the Codecademy Forums!

Try looking at this thread to see if it solves the problem.

Why is the " " in between the O not part of the index? “O O O O O” would be index 0:9 right?

It’s not part of the index because it is not included in the list, only the print out.

``````>>> o = ['O'] * 5
>>> print (*o)
O O O O O
>>>
``````

But here for example, I did " ".join() just like in the battleship lesson and the empty space is indexed…

``````>>> a = ["0", "1", "2", "3", "4"]
>>> " ".join(a)
'0 1 2 3 4'
>>> " ".join(a)[1]
' '
>>> " ".join(a)[2]
'1'
>>> " ".join(a)[3]
' '

``````

Never mind, I figured it out.

It’s because in the lesson, the variable was never updated to be " ".join() so we’re still calling the variable when it is a list. Totally missed that part.

1 Like

The above is a subscript. Strings are subscriptable and since the space is inserted then it will have an index. When we print directly with a spread operator the space character is inserted on every comma separated value in the list.

``````>>> print (*["0", "1", "2", "3", "4"])
0 1 2 3 4
>>>
``````
1 Like

i feel like it would be better if the lab wanted `board[guess_row - 1][guess_col - 1] = 'X'` to mark missed guess, assuming a ‘player’ would not count rows/columns starting at 0 like indices. that’s more intuitive to how a real person would play this as a command line game