13/19 - Why the "You guessed that one already." let me pass?


#1

The code below is good except the elif that checks for existence of an "X" at the intersection of guess_row and guess_col

elif board[guess_row][guess_col] == "X":
print "You guessed that one already."


The elif above can not see any "X" because and "X" is not written on the board at any point before the elif. But the website says I'm good to go. Why is that?

Here's the full code:

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)

guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))


def random_row(board):
    return randint(1, len(board))
ship_row = random_row(board)


def random_col(board):
    return randint(1, len(board))
ship_col = random_col(board)

print "Ship row: " + str(ship_row)
print "Ship Col: " + str(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."
 elif board[guess_row][guess_col] == "X":  <<<<<------------------LOOK HERE
        print "You guessed t:eyeglasses:
else:
            print "You missed my battleship!"
            board[ship_row - 1][ship_col - 1] = "X"
            board[guess_row - 1][guess_col - 1] = "Y"
            print_board(board

)


#2

Not sure python understand that print statement... :smile:

There's also a problem with your closing parenthesis here...

I'm surprised the code validator has let you get away with these two errors..?


#3

Also, you need not to worry about the X being set before or after in your function. Right now this game only has one turn but later on you will implement multiple turns and calling the same function multiple times. If during a given turn you guess wrong, you will store X into board and then your elif will prove useful.


#4

please tell me why you subtract 1 in :

>             board[ship_row - 1][ship_col - 1] = "X"
>             board[guess_row - 1][guess_col - 1] = "Y"

#5

@bitplayer38409:

I'm assuming its because he's defining a range(0,5) for his board but using a range of 1 to len(board) to generate his random integer for random_row and random_col...