Test run


#1



Test run


When I enter coordinates outside the acceptable terms, I get:

Guess Row: 2
Guess Col: 7
2
4
You missed my battleship!
Traceback (most recent call last):
File "python", line 38, in
IndexError: list assignment index out of range

rather then "Oops, that's not even in the ocean." I assume this is because it's trying to add "X' to a location on the list that does not exist... Also, any valid guess prints "You've already guessed that!" .. would love some help


# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
else:
    print "You missed my battleship!"
    board[guess_row][guess_col] = "X"
    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:
        "You missed my battleship!"

    
    print_board(board)


#2

Apologies, after advancing in the lesson the code seems to work fine.... not sure what changed.


#3

Actually, three sets of parenthesis in addition to the formatting of the winning condition was changed. Can someone explain why this fixed the game?

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"
if turn == 3:
print "Game Over"
print "Turn", turn + 1


#4

They don't, your observation that they do is incorrect. You can easily verify this by removing them.

The reason for why it shouldn't crash is that you should be testing for whether the input is valid, and only then use it.

If you use the input before testing whether it's valid, well, expect it to crash on invalid input - the above reason wasn't implemented.


#5

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