Hide Excercise


#1

Hello Friends,
I am strucked with Hide exercise of BattleShip. I have no clue of whats wrong. Please help me.

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)


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

It give me the error as Oops, try again. Did you create a function called random_row? and also indication that
NameError: global name 'randit' is not defined


#2

Look at what that NameError says.

also, your random_col picks a random row.. which "works" if the board is square, but not so much if the width is different from the height.


#3

@ionatan
I have generated the grid, not clear why it says the error Oops, try again. Did you create a function called random_row and error and NameError:global name randit not defined. The 5X5 is as below. I have no clue of sorting this. Can you be more clear about this?

O O O O O
O O O O O
O O O O O
O O O O O
O O O O O


#4

@ionatan
I have corrected the code by calling the print_board(board) function which is missing from my earlier code. The newcode is as below.

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)

It prints the below output and some errors

O O O O O
O O O O O
O O O O O
O O O O O
O O O O O

Traceback (most recent call last):
** File "python", line 19, in **
** File "python", line 14, in random_row**
NameError: global name 'randit' is not defined


#5

You posted wrong code or wrong error message, because that code isn't going to trigger that error message.


#6

@ionatan
yeah you are right. I have written the code right, but copied the errors from my earlier code into it. One more thing why we need to put the randint(0,len(board[0])-1) instead of randint(0,len(board)-1). I have checked with both versions,and they both are generating the 5X5 grid.


#7

Change the grid to 5x10 and see what happens.


#8

@ionatan.I have changed the statement to make it 5X10 grid.It says that** column bounds are not correct.** My doubt is why we need to put return randint(1,len(board[0])-1) instead of return randint(1,len(board)-1)

def random_col(board):
    return randint(1,10)

#9

You've got two functions. One that picks a row index and one that picks a col index.

If those functions are identical, then they aren't going to work for a non-square board.

What they should do is to inspect the board to figure out its size.


#10

@ionatan. ok.So they should be different to work for a 5X5 square board. I think len(board[0]-1) is needed.because the first row index is o and the the first coulmn index in that first row is board[0]-1.please correct me if i am wrong

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

#11

board is a list of lists.
Each list in board represents a row.

The height is the number of rows, which is the number of elements in board
The width is the length of a row which is any element of board

[['O', 'O', 'O', 'O', 'O'],
 ['O', 'O', 'O', 'O', 'O'],
 ['O', 'O', 'O', 'O', 'O'],
 ['O', 'O', 'O', 'O', 'O'],
 ['O', 'O', 'O', 'O', 'O']]

#12

@ionatan ok. So the width is length of row which is nothing but the column index of an element in the board

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

#13

This works fine for me.

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)

Add your code below!

from random import randint

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

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

return random_col(board)
return random_row(board)

#14

This Worked for me as well:

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)

Add your code below!

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

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

random_row(board)
random_row(board)


#15

@ionatan. Thanks for the clarification. I had completed the exercise and moved on to the next stage