# Questions about 11. Danger, Will Robinson

#1

``````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

if guess_col == ship_col and guess_row == ship_row:
print ("Congratulations! You sank my battleship!")
else:
print ("You missed my battleship!")
board[guess_row][guess_col] = "X"
print_board(board)``````

Hi guys, I have a couple of questions regarding this battleship program.

a. In the random_row and random_col functions, why is that -1 needed? What does it do? Isn't the function just meant to take a random integer from 0 to the length of the list (5 indexes)? So wouldn't it need to be +1 so it becomes 0, 5 and able to pick from between 0, 4 (which is 5 indexes)? - 1 would make it 0,3, meaning its only able to pick from 0,2 (3 indexes.).

Whats the meaning behind this? It's really frustrating.

b. My "X" does not seem to appear on the correct coordinates. Why is that?

I'd much rather be helped with question a than question b. Help, please?

Much appreciated

#2

a. the board has multiply list, but all lists have a length of 5. Which means the indexed are: 0,1,2,3,4 (list are zero indexed based, counting starts from zero). So, if you generate a random number:

``return randint(0, len(board))``

that could give: 0,1,2,3,4 and 5. Oops, we don't have a index 5. Which is why we have minus 1.

b. the coordinates are fine, just start counting from zero.

#3

But my list has 5 indexes, correct? So...(0, len(board)) would be 0, 5? Sorry if I am wrong, but I thought the 5 wouldn't be included to begin with.

eg. [0,10] would give 0,1,2,3,4,5,6,7,8,9 but not include 10? So why the -1 if they 5 wouldn't be included? It would only count 0,1,2,3,4 and not the 5.

Again, sorry for any hassle, and your help is much appreciated.
Thanks.

#4

yes, it has 5 indexes and counting starts from zero.
0,1,2,3,4 are 5 indexes. 0 (1), 1 (2), 2 (3), 3 (4), 4 (5).

lets do an example:

``hello``

has 5 letters:

``````h   e   l   l   o
0   1   2   3   4``````

so trying to access the letter at the 5th index:

``"hello"[5]``

would be a problem the 5th letter is at index 4 because counting starts of zero

if you do: randint(0,5), the possible outcomes would be: 0,1,2,3,4 and 5

#5

Ok, if I write like, randint(0, 5) It WILL include the 5, right? So, 0,1,2,3,4,5.

One more thing, since the length of my list is 5 indexes, when I do [0,len(board)] the len(board) becomes 5 because thats how many indexes I have in my list? So, 5 indexes will make it equal 5 as opposed to 4 (0,1,2,3,4)

Thanks.

#6

yes, i just run a 1000 random generated numbers (`randint(0,5)`) to be sure. 5 is included.

the length of your list is 5, and the indexes are at: 0,1,2,3,4. So you don't want to generate 5, since that is outside your list. 0,1,2,3,4 are 5 indexes, not 4!!! please count careful.

#7

Hahaha, thank you sooooo much. You've been a lifesaver. It was really frustrating me cause I couldn't figure it out.

Thanks a lot

#8

This proves counting to 5 can be difficult.

#9

Haha, I got that it started from 0 and went onwards. It's just that earlier, with one of pythons built-in functions (I forget it at the moment), if you put in say [0,5], it would do 0,1,2,3,4 and ignore the 5. I thought the same rules applied to the randint as well, so something wasn't adding up, but you cleared it up for me, so all is good