#1

Hello friends,

I am strucked with Aim exercise of battleship. When i give the correct range say for instance 4,4. It is displaying the corrrect row and correct column along with the message You missed. Also, it displays en error message that**_ Oops, try again. Make sure you print "Oops, that's not even in the ocean." if the user guesses a row or column that is off the board._**

when i give the incorrect range like 6,6 it is displaying the message _Oops,that's not even in the ocean._ in the output and still giving error message that Oops, try again. Make sure you print "Oops, that's not even in the ocean." if the user guesses a row or column that is off the board. I did not understand why it is displaying like that even though i printing that message when i give incorrect range. I request all friends to help me clear on this

``````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 not in range(5) or guess_col not in range(5)):
print ("Oops,that's not even in the ocean.")
else:
print("you missed")

board[ship_row ][ship_col]= "X"
print print_board(board)```````

#2

Hi,
let's start with fixing your typo, where you print a "X". Because you print an "X" to the position of the ship, and not of the shot, the user guessed

It should've been:

``board[guess_row][guess_col] = "X"``

But, it won't work again, because you want to print it after you checked if it's in the ocean or not, so you will get an error if you guess outside of the field, because it actually can't print in that position, which doesn't exist

It would be better if you put it right under the if statements, where you want to print a "X", like:

``````# Write your code below!
if(guess_row == ship_row) and (guess_col == ship_col):
print "Congratulations! You sank my battleship!"
board[guess_row][guess_col] = "X" #You could put one here too

elif(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")
board[guess_row][guess_col] = "X"``````

I hope it helps

#3

@frozentofu I have corrected the as per your suggestion. I still get the same issue. I changed to board[guess_row][guess_col]="X" and executed. I have had the same issue which i specified earlier. Moreover, i understand that the board[ship_row][ship_col] is actually the position of the ship generated from the ship_row = random_row(board)
ship_col = random_col(board) which is not the position user guessed. Please clarify and find the below modified code.

``````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!"
board[guess_row][guess_col]="X"

elif(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")
board[guess_row ][guess_col]= "X"

print print_board(board)``````

#4

@frozentofu. I have corrected the mistake and moved on to the next exercise,Thanks

#5

You're welcome! I'm glad that I could help