Help on 11/13


#1

This is my code:

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) - 1)

shiprow = random_row(board)
shipcol = random_col(board)
guessrow = int(raw_input("Guess Row:"))
guesscol = int(raw_input("Guess Col:"))

print shiprow
print shipcol

#Write your code below!
if (shiprow == guessrow) and (shipcol == guesscol):
        print "Congratulations! You sank my battleship!"
else:
        print 'You missed my battleship!'
        board[guessrow][guesscol] = "X"
        print_board(board)

I get this error -
Traceback (most recent call last):
File "python", line 33, in
IndexError: list index out of range
Can someone help please?????


#2

Only input numbers in the range 0-4 (inclusive) This problem for failing with invalid numbers is fixed in the next exercise - https://www.codecademy.com/en/courses/python-beginner-en-4XuFm/1/3


#3

when you guess, keep it in the range for column and row. If you put the user input outside the row or col, it will give you and error. do user input 4 and 0..


#4

The last print statement -- print_board(board) -- should be in the separate line not in the --else-- case.


#6

@bin123 no this is not the OP's problem.