13/15 Remember Error


#1
Here is the code:
for turn in range(4):
    print "Turn %s" %(turn+1)
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))
    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
    else:
        if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
            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"

The error is:

Oops, try again. Did you remember to print out turn + 1 each turn?

I added, didn't I?


#2

Check if

has the same indentation as lines below
it seems like it doesn't


#3

That's my mistake @dawidrz87 while pasting. Indent is correct, I checked once more.


#4

The original provided code had a comment toward the end that indicated where and what to write. Move this line down to there, and follow the original instructions. Don't force code onto the project that is not asked for.

print turn+1

You may wish to try sting formatting later, but first try to get the code to pass as close as possible to the instructions.


#5

The answer is as you described @mtf .

The asked code is:At the beginning of each iteration, print "Turn", turn + 1 to let the player know what turn they are on.

There is a print "Turn" statement, that's why I added that part. But, problem solved, thanks.

The solution is:

print turn+1

instead of

print "Turn %s" %(turn+1)


#6


My above code works. But I have a question:
Why is it OK to define "board[x][y]" as "X" AFTER my elif statement? Is computer programming sequential?


#7

That line changes a grid point from an "O" to an "X".

In practical terms, it is actually board[row][col], so your representation could be written as board[y][x]. The first index is the row, the second index is the column (or row of internal list).


#8

Thanks for responding :slightly_smiling:
But why is it that I can type in "elif board[row][col] == 'X': print 'You guessed that one already.'" before I can type in "board[row][col] = 'X'"? I am defining board[row][col] after I tell Python to look for a similarity between board[row][col] and X and that is acceptable, but I thought that programming languages were sequential and thus, say, defining variables had to occur before calling them? Is this not the case with Python?

Thank you so much :smile:


#9

The values in the list are defined when we build the board. They are strings. It makes sense to check if a value has been altered already, before altering it. In other words, if it is a string, 'X' then that 'square' has been chosen already, and was a miss, so marked with 'X'.