14/19 never get "already guessed"


#1

With the following code, I never get the "You guessed that one already" message, even if I run it two consecutive times with the same exact inputs. Can anyone tell me what's wrong?

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

You're code never prints already guessed because you didn't add the for loop yet. (The next exercise adds this.) As of right now, your code lets you guess once then stops. When you run it two consecutive times with the same exact inputs the code resets itself between each time. So if you run it and guess 1 and 3 and the board changes that spot to an X, but the next time you run it the board will reset itself and will once again be all Os.


#3

OK, thanks. I thought it might be something like that, but the instructions confused me by asking me to check and see if it was working.


#4

No problem :smile: could you press the button on my answer that says "accept answer" or "solution" to show that this question was answered? Thanks