"List index out of range" error on lesson 12. Bad Aim


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

print ship_row
print ship_col

# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
else:
    board[guess_row][guess_col] = "X" 
    print print_board(board)
    if guess_row.range(5) or guess_col.range(5):
        print "Oops, that's not even in the ocean."
    else:
        print "You missed my battleship!"
        board[guess_row][guess_col] = "X"  
        print print_board(board)

The error code I receive is:
Traceback (most recent call last):
File "python", line 32, in
IndexError: list index out of range

I'm having great difficulty trying to understand this error, but maybe I'm over thinking it.


#2

I was having the same problem, it was printing the "Oops, that's not even in the ocean." and still giving the error.
Passed me when I guessed in the range though.


#3

same like you ryan. any body knows how to solve this?


#4

Did either of you solve the issue you were having?


#5

When you click submit try entering values that are below 5 for example 2 and 3 that worked for me.


#6

if guess_row/col NOT IN range(whatever):

if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank 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."
else:
print "You missed my battleship!"


#7

You are receiving this error because when you enter a value out of range, say 5 for row , the code is not not checking the condition :

 if guess_row not in range(5) or guess_col not in range(5):
     print "Oops, that's not even in the ocean."

Instead it reaches to your else statement, board[guess_row][guess_col] = "X" where it throws ,
IndexError: list index out of range

So make the above mentioned if statement as the first or the second condition to be checked, see my code below which worked:

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

print ship_row
print ship_col

# Write your code below!
if guess_row==ship_row and guess_col==ship_col:
    print "Congratulations! You sank my battleship!"

elif guess_row not in range(5) or guess_col not in range(5):
    print "Oops, that's not even in the ocean."
else:
    print "You missed my battleship!"
    board[guess_row][guess_col]="X"
    print_board(board)

#8

Your "board[guess_row][guess_col] = 'X'" is before checking if the input number is out of range. So using a number out of range will make your code try to put a 'X' in a columm and row out of range.

(English = non-native language) :slightly_smiling:


#9

I had a similar error when I keyed in a number larger than 4.
It returned: "Oops, that's not even in the ocean." + Error message: IndexError: list index out of range.

Check your indents. the out-of-range message was correct, but I called
board[guess_row][guess_col] = "X"
print_board(board)

the last two lines as a global input, rather than a local input under my else: condition

Problem solved :joy:


#10

Thank you! I reversed the order and it worked! I was a bit confused as to why that order is so important since I was not trying to input X at the guess coordinates, but even the check itself can give that error if you're checking something that doesn't exist! Thank you