# Play it! Sam

#1

Hi, Whats wrong with below code. Output is generated, as expected (I believe). Still an error?

``````from random import randint

board = []

for x in range(5):
board.append(["O"] * 5)

def print_board(board):
for row in board:
print " ".join(row)

print "Let's play Battleship!"
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):
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
print ("Turn", turn + 1)
if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sunk 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)``````

=============================
OUTPUT -

Let's play Battleship!
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
0
2
Guess Row: 3
Guess Col: 3
('Turn', 1)
You missed my battleship!
Guess Row: 1
Guess Col: 2
('Turn', 2)
You missed my battleship!
Guess Row: 0
Guess Col: 5
('Turn', 3)
Oops, that's not even in the ocean.
Guess Row: 7
Guess Col: 8
('Turn', 4)
Oops, that's not even in the ocean.
O O O O O
O O X O O
O O O O O
O O O X O
O O O O O
None

==================================

ERROR -

Oops, try again. Did you remember to print out turn + 1 each turn?

#2

#3

#4

No problem, now back to your code,

All you need to do is move this line to the end of your code,

But makes sure that it is on the same indentation level as your if/elif/else statements

#5

Thanks!

1) Ohh, but as per instructions, I put at the begining - At the beginning of each iteration, print "Turn", turn + 1 to let the player know what turn they are on.

2) As per your suggestion - I made the change, same error.

``````for turn in range(4):
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 sunk 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 ("Turn", turn + 1)
print_board(board)``````

============
ERROR-

Oops, try again. Did you remember to print out turn + 1 each turn?

#6

Same exact issue here. Help?

#7

type this line,
print "Turn", turn+1
just after u have written for loop for iteration to provide 4 guesses..
It worked for me.

#8

Should be on the same indentation level as your first if/else,

#9

I had the same problem, but I got rid of it by removing the "Turn" from the print statement.

``````if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sunk 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(turn + 1)``````

#10

That would be in addition to the first if including a break statement so the game loop terminates.

#11

This worked for me:

for turn in range(4):
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
print "Turn", turn + 1

I tried it with the print command in a few different places, but this satisfies the request for it to be at the beginning of the loop and the specific formatting codecadamy seems to be looking for.

#12

Try the print function without the parenthesis. I was having the same problem until I removed them, then worked fine.

#13