Bad Aim!


#1



When user guesses a column or row off the board, correct message ""Oops, that's not even in the ocean."is displayed,yet,an error message pops up. Not able to debug

Error Message: 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.


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

# Write your code below!
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 my battleship!"
    board[guess_row][guess_col] = "X"
    print_board(board)


#2

Hi @strivehard135 ,

Precede all of this code ...

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 my battleship!"
    board[guess_row][guess_col] = "X"
    print_board(board)

... with this line, and then indent the code ...

else:

In this line ...

elif guess_row not in range(5) or guess_col not in range(5):

... change the elif to if.

These changes are designed to prevent this line from executing when the user's guess is out of the range of the row or column numbers ...

board[guess_row][guess_col] = "X"

#3

I think this error message is simply referring to the fact that you didn't use a . at the end of the word ocean. That's the message I get at least, when I tried removing the dot.


#4

You are correct, @digitalninja65206 , regarding the reason for the message that Codecademy displayed.

@strivehard135 , in addition to adding the necessary ...

... also note that the instructions include this ...

Add a new if: statement that is nested under the else.

Be sure to structure your code to conform to that instruction and the ones that follow.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.