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.
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]
(I’ll try to explain, I hope you’ll understand my english )
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?
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.
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:
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.