"You guessed that one already."


#1



https://www.codecademy.com/courses/python-beginner-en-4XuFm/1/5?curriculum_id=4f89dab3d788890003000096
I cannot seem to end up with "You guessed that one already." after I repeat an answer. It simply prints "Congratulations! You sank my battleship!" if my values are within the required range.

What might the matter be?


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."
    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)


#2

The reason your code isn't working is because, at this stage of the game, you only have one try at guessing the row and column, and so when you start the code again, it won't work because the code resets, it again appends five ["O"] to the "new" board array. And so you won't end up with "You've guessed that already" yet, if you test your code out when you implement the mechanism to give you four tries, then that code will come into play

Regards,
Barry Allen


#3

Thanks Barry. You are absolutely right. Patience, patience...


#4

Haha yes. Patience is a virtue, especially to programmers like us. Keep trying your best.

Regards,
Barry Allen


#5

Barry, a new weird thing is happening: in my console, just after the first grid of "O"s, a valid row-column combination is on display. Any ideas on how to fix this? Here's what I mean:

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
2
1
Turn 1
Guess Row: 2
Guess Col: 1
Congratulations! You sunk my battleship!
Turn 2


#6

What exactly do you mean by a valid row-column combination. If you're referring to the fact that it gives you the answer right after the grid of "O"s then that is because of the two print statements
print ship_row
print ship_col

If you remove these lines from you code, then it will stop displaying that.

Regards,
Barry Allen


#7

I felt a little silly about that. Thanks, though. :sweat_smile: