A general question about the random_col stuff~


#1

I feel like maybe I missing something pretty important. (Like I’m learning how to do things but not why I’m doing them?) I’m on 11/19 (Danger, Will Robinson!!) and I’m going through my code and I don’t really understand this part (line 14-18 at this point):

def random_row(board):
  return randint(0, len(board) - 1)

def random_col(board):
  return randint(0, len(board[0]) - 1)

I don’t know why I put the index on the random_col and I don’t understand why I subtracted 1 from both of them. Keep in mind I’m pretty new to this whole programming thing so I probably won’t understand abstract concepts or anything.

Anyway, thanks for any help you can provide.


#2

The index is for the first row of the two dimension list. With the board being square it is rather moot, but if the board dimensions are not the same, say 10 wide, 5 high, we would need the actual length of the row.


#3

Since row and col are kind of arbitrary assignments and aren’t actually functions built into python, how does the code know whether it’s assigning a random integer vertically or horizontally?


#4

By the index position…

board[row][col]

   c o l u m n s
r  O O O O O O O
o  O O O O O O O
w  O O O O O O O
s  O O O O O O O

#6

I still don’t really get it but I don’t think you need to keep trying to explain. I don’t think I’m going to understand it with my pretty limited knowledge of the subject. I know what rows and columns are, I just don’t know how you figure it out w/ the index. Thanks for trying to help though.


#8