FAQ: Learn Python - Battleship - Play It, Sam

faq

#1

This community-built FAQ covers the “Play It, Sam” exercise in Codecademy’s lessons on Python.

FAQs for the Codecademy Python exercise Play It, Sam:

Join the Discussion. We Want to Hear From You!

Have a new question or can answer someone else’s? Reply (reply) to an existing thread!

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

Need broader help or resources about Python in general? Go here!

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

Learn more about how to use this guide.

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!


#2

Other FAQs

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 (search) in the top-right of this page. Still can’t find it? Ask it below by hitting the reply button below this post (reply).


#4

2 posts were split to a new topic: ValueError: invalid literal for int() with base 10: ‘SCT’"


#6

2 posts were split to a new topic: Program will only print “that’s not even in the ocean” no matter what


#7

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

for turn in range(4):
print “Turn”, turn + 1

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!”
else:
if(guess_row < 0 or guess_row > 4) or (guess_col < 0 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”
print_board(board)

why this error is comming ,how to solve it
Traceback (most recent call last): File “python”, line 25, in <module> ValueError: invalid literal for int() with base 10: ‘RUN’


#8

Why do I keep getting an indentation error? After trying to solve this issue for a while I asked for the solution and had it correct. I reset the exercise and put in all the correct information to see what I was getting wrong. I got an indentation error from Line 29 down. I would delete (or backspace if it makes a difference) to the previous line and hit enter. I then compared that with the solution (I took a snippet so I had it handy) and set the alignment exactly where it was suppose to be (which was literally exactly where I had it before deleting) and rerunning the program. This indentation error then moved to line 30. Rinse and repeat. Now on level 37 it keeps appearing and wont change. Indentation appears right on my end and matches the solution provided. What gives? Am I missing something here or is this a bug?


#9

Now I’m even more frustrated. I just posted this and the format that i see on Github does not match what I see in the Codecademy IDE (if that is the proper name for it. Here is exactly what I see and what I see on the github link I posted.

image

image

So after seeing this I go back to Codecadamy and type in a space. Doesnt work. Try another space. Doesnt work. Try another space. It works. Now the indentation error was on 39. Same thing for that. Repeat it on line 41 as well.

Any reason it is appearing differently between the two causing this error?


#10

The codecademy editor settings are not ideal, i recommend you go to repl.it, codepen or any other site, and indent your code there (or in a editor like atom or vs code on your computer)