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) - 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 # Write your code below! 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?