Battleship/Game Over: int() error - FIX(?)


#1

I want to share this fix that I found for this exercise, maybe it will help illuminate the bug:

For the following error:
Traceback (most recent call last):
File “python”, line 28, in
ValueError: invalid literal for int() with base 10: ‘RUN’

The problem seems to occur in the for loop, once the Turns have been implemented.
My code looks like this:

for turn in range(4):
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 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”
if turn == 3:
print “Game Over”
print_board(board)
# print turn + 1 here
print “Turn”, turn + 2

NOTICE that I changed the turn + 1 at the end to be turn + 2 - that triggers this error.

My fix is to change the line (bold above):

From:
elif board[guess_row][guess_col] == “X”:

To:
elif board[guess_row][guess_col] == ‘X’:

I don’t know why but the single quotes made it run.


#2

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