What does list index out of range mean?

Question

What does list index out of range mean?

Answer

We haven’t done any error handling in our code yet to prevent it from crashing if you enter invalid input, so be sure to only provide valid row and column. Remember, indexes start from 0 in Python, so we have rows and columns numbered 0 through 4.
If you guess something like row 5 and col 5, it will give you an index out of range error because board has no index 5.

3 Likes

i dont understand this line of code : board[guess_row][guess_col] = “X” in this exercise, im getting confused…

3 Likes

to update an element in a list we have the following syntax:

list[index] = "new value"

however, board is a list which contains list (so nested lists if you like). So to access the inner lists and update an element, you need: [outer_list_index][inner_list_index]

10 Likes

oh, ok thank you ! :grinning:

1 Like

I have two lines of input, but when I run the code it takes the first input and duplicates it to the second, does anyone have an idea what to do?

guess_row = int(input("Guess Row: "))
guess_col = int(input("Guess Col: "))

output:
0
4
Guess Row: 1
Guess Col: You missed my battleship!
1
1

1 Like

@objectwhiz27592, be sure to use “raw_” in front of your “input”.

guess_row = int(raw_input("Guess Row: "))

1 Like

Help
What is wrong with my code?
image

x = [guess_row, guess_col]

if guess_row == ship_row & guess_col == ship_col:
print “Congratulations! You sank my battleship!”
else:
“You missed my battleship!”
print print_board(x[guess_row][guess_col])

In English, can you describe to me what your code does?

If I look at your code, I would say you create a new list with the guessed coordinates (not sure why).

then this line:

x[guess_row][guess_col]

you do two index look ups, like the list is multi-dimensional. Its not. the list contains two values.

the result of this action is then passed as argument to print_board.

(I’ll try to explain, I hope you’ll understand my english :slight_smile:)
The task is to create a version of the game “battleship”.
the board is made of 5 rows, each contains 5 “O”, The player need to guess where the ship is hiding.
If he fails I want the code to print the new board with “X” where the player already guessed.

That is what the code needs to do. But that is not what your code currently does

So lets break this down together: How do we update an element in the list? After we gained the guessed coordinates, how do we use these to update the board?

I’m trying to think…

The board is like a list of lists, the outer one is making the rows, and the inner one is making the col.
I just need to change it according to the guesses, i think.
I’m not sure how.

then take a step back, if we have flat list:

example = ['O', 'O', 'O', 'O', 'O']

how do we replace a 'O' witn an 'X'?

example[index] = “x”
(?)

Very good :slight_smile:

so now if we have nested lists, doing example[index] would give back another list. So then we can once again use another index to get the value from that list:

example[index][other_index]
1 Like

okey :slight_smile:
image

I improve my code a little, but it still doesn’t working…

If updating an element list has the following syntax according to you:

example[index] = "x"

why do you use a comparison operator (==) in your code?

updating your board and printing your board are two steps, why do you attempt to do this in one step?

if you ever decided to make your board a rectangle (rows != cols), your check at line 29 is not sufficient.

I succeeded, thanks!

Why is guess_col the outer list and guess_row the inner list?

This has to do with the way the board is structured.

our board is a multi-dimensional array. The list contains other list, each of these lists is a row on our board. Within these nested lists we have our columns.

My code keeps throwing the following error after a while that I run it:
ExecTimeoutException: Program took too long to terminate.

I have already checked my loops and they look fine. There is something going wrong in the browser or something Idk