(13. Not Again.) My code works but it seems illogical


#1



https://www.codecademy.com/courses/python-beginner-en-4XuFm/1/4?curriculum_id=4f89dab3d788890003000096#


My code works! I want to know why.


I expect it to fail because the part that marks my guess is AFTER the code that checks if the guess has already been made. How can it check for what's not yet there?

Just want to understand. All help appreciated.


# 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

i am a bit confused by your question, but let me try to clarify.

you are confused by this line:

elif board[guess_row][guess_col] == "X":

right? From a logic point of few this make sense right? You want to check if the user doesn't guess something it already guessed, before approving the guess

but don't forget, the board is already made, so if the user hasn't guessed this spot already, it will contain a O, so then the condition will be false:

"O" == "X"

which is false.


#3

Hi, @m.mato and @stetim94 ,

As of the Not Again! exercise, the user still only gets one opportunity to guess the location of the battleship. This has been a source of confusion to some users, since, if the user has yet not made a guess, then testing for the location of a previous guess makes no sense. This is probably the reason for @m.mato 's concern.

In later exercises, the instructions ask the user to create a loop, which offers an opportunity to submit up to four guesses. At that time, the test that checks the location of the user's earlier guesses will become useful.


#4

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