11/13 Danger, Will Robinson! - It works, but WHY?


#1

In the last else statement I did this to change the guessed position to X:

board[guess_row][guess_col] = "X"

Can anybody explain how this works? I thought i was storing 5 separate lists, how does the code recognize the distinction between rows and collums?


#2

There are two indexes for a 2-D array: guess_row and guess_col.

 O O O O O
 O O O X O     # X is at board[1][3]
 O O O O O
 O O O O O
 O O O O O

#3

The variable board[guess_row][guess_col] = "X" knows where to print the X because it gets the value from bellow code:

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

Then using the ELSE the code print the X based on the information that you inputted previosly.


#4

Hi webrockstar43625

board[guess_row][guess_col] = "X"

is saying

find the element in the list board at the index == to guess_row
and then within the element at guess_row locate the element == index guess_col
then assign "X" to that location.

for example if given the list
a = [["a1", "a2", "a3"], ["b1", "b2", "b3"], ["c1", "c2", "c3"]]

print a
will print the whole list: [["a1", "a2", "a3"], ["b1", "b2", "b3"], ["c1", "c2", "c3"]]

print a[1]
will print index 1: ["b1", "b2", "b3"]

print a[1][2]
will print the 2 from index 1: "b3"


#5

Thanks, that's exactly what I needed!