12/19 Bad Aim - NEED HELP


#1


Hey, does anyone know why the code is not working.
It's giving me this error: "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."

Thanks for the help


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!"
else:
    if  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

Compare the above to your string. They must be an exact match.


#3

For future lessons, do make sure to do exactly as the lesson tells you to as the lesson checker has certain specifications it wants in your code when checking it, and even though your code may technically be correct, it won't think so unless you have those specifications. Small differences like that are the most common, and sometimes you'll spend hours trying to spot it, so try to prevent it beforehand by following the instructions closely. Often times when they want a specific variable, string, etc. they will have the object written in code format like they did in the lesson. Pay special attention to that.


#4

Add a new if: statement that is nested under the else.mentioned
it means make use of elif

what ever you have written right,small mistake.This is what you have written

else:
    if  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'

this is what i am suggesting

elif guess_row not in range(5) and 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)

#5

Additionally, a lot of times copying and pasting the string that they want is the best option. It saves time and typos.


#6

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