Test run


#1
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)
print ship_row
print ship_col
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))



# 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"  
    print_board(board)
    
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)

this is my code and when i test it for my guess being not on the board this is what i get:

Traceback (most recent call last):
File "python", line 35, in
IndexError: list assignment index out of range

any help would be greatly appreciated.


#2

You are missing a colon at your first else: statement:- (board[guess_row]...


#3

excuse my ignorance but i have no idea what you are trying to show me.


#4

else:
[tab]print "You missed my battleship!"
[tab]board[guess_row][guess_col] = "X"
[tab]print_board(board)

1) You have this part two time in your code
2) Your second "if" should be related to "else" statement, because you are widening your exception logic
So you should have something like this:
if statement == true:
[tab]print ""
else:
[tab]if statement not in range:
[tab][tab]print ""
[tab]elif statement == prev_statement:
[tab][tab]print ""
... and so on. (sorry for my poor english)


#5

like this?

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"
print_board(board)
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)

#6

The problem with this exercise as it is written is that either it's not clearly written or it is designed poorly.

Any time:
board[guess_row][guess_col] = "X"
is executed before testing if the guess is within the bounds of the board (zero to 4), it will throw an "index out of range" exception.

What does that error mean? Valid indexes for our board are board[0][0] to board[4][4]. If your guess is at board[7][22] then when the program tries to place an "X" at board[7][22] (which doesn't exist, remember), you will get an index out of range exception.

For this program to make sense (how it is written, not how you personally have coded a solution), the check for indexes that are out of range should really come first or second. They don't have us write a solution that way, so people are bound to come up with errors like this.

Ideally, you would check for a hit first, then check out of range (if guess_row not in range(5) or guess_col not in range(5):), then check the other stuff. This will prevent the index out of range error.