11. having list index out of range error


#1



https://www.codecademy.com/en/courses/python-beginner-en-4XuFm/1/2?curriculum_id=4f89dab3d788890003000096


When I run my program and enter my guesses I have that kind of error:
Traceback (most recent call last):
File "python", line 33, in
IndexError: list index out of range


Anyone has clue what I did wrong?


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:
    print "You missed my battleship!"
    board[int(guess_row)][int(guess_col)] = ["X"]
print_board(board)


#2

this line seems to be the problem:

board[int(guess_row)][int(guess_col)] = ["X"]

you already have a multi-dimensional list, you can just insert "X" there, no need to insert another list


#3

So I changed

board[int(guess_row)][int(guess_col)] = ["X"]

to

board = ["X"]

and now I'm receiving

Oops, try again.
Make sure to enter a col and row that is on the board!

No matter what data I enter


#4

no, the left side of your equal sign was fine. I was saying you should just insert a string X into the multi-dimensional list

you currently insert a list containing X into your multi-dimensional list.


#5

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:
    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!"
    board[guess_row][guess_col] = "X"  
    print_board(board)

i am receiving similar error, ,even after making the correction that you were talking about.


#6

your code is fine, what coordinates do you enter? Can you show me a screenshot or something?


#7


#8

you have a 5 by 5 field, which is zero indexed based (counting starts at zero, so: 0,1,2,3,4) of course the numbers you entered are out of range, enter valid numbers. Your code is not yet designed to handle numbers which are outside the field


#9

Yes , right, code must be simple:

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

#10

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