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 ().
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?
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)
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).
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.
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.
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.
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
Hi, I was experimenting with a different logic than the exercise proposed. Exercise:
for x in range(5):
board.append([“O”] * 5)
Mine:
for i in range(5):
rows.append(‘O’) # I’ve defined rows = previously.
board.append(rows)
Mine 2:
for i in range(5):
rows.append(‘O’) # I’ve defined rows = previously.
for r in range(5):
board.append(rows)
The end result at first is the same (a clean 5x5 grid of “O”'s), BUT, when I arrive to the “X” part, the “X” repeats itself in all rows (but not columns). And I did not understand why.
f guess_r == ship_row and guess_c == ship_col:
print “Congratulations! You sank my battleship!”
else:
print “You missed my battleship!”
board[guess_r][guess_c] = “X”
Mind how you construct the two-D list. Each row must be independently constructed. We cannot append the row five times since it is only a single reference object.