Why does it ask if I've created the random_row or random_col function?

Question

Why does it ask if I’ve created the random_row or random_col function?

Answer

If you’ve misspelled the name of the randint() function that we use to generate a random number, that will cause this error because Codecademy is testing the return value from your function.
Another common issue is not returning the random value you generate, so be sure to do that!

def random_row(parameter_name):
  # return random value here
3 Likes

Is it okay to have the argument in a function as the same name as the variable named above? It just seems a bit strange to do this since it is normally different:

board = []

for x in range(0, 5):
  board.append(["O"] * 5)

def print_board(board):
  for row in board:
    print " ".join(row)
1 Like

It is okay, as you are merely naming an argument for that function.

However to make the code less confusing, you may want to make the name of the argument different from the name of the variable board that you have created already.

3 Likes

can you combine the random_col and random_row definition and/or calling into one line? since they have the same function and argument passed.

1 Like

the body of both functions should be different, because this allows you to have a rectangular board (6 * 8) for example.

so its good to have two separate functions.

2 Likes

Hey please help, I am just trying to print a Board of 6*5 Dimension

and I am writing the code like above in image

in this case to generate the random row and column , how will the length of Board will be calcuated ?

Will it same for both function random_col and random_row

Looking for reply

2 Likes

you now have 6 rows, and 5 columns.

your random_col could now generate 5 as index, which could give an index error

to calculate the length of the columns, you would need to access the first row

Hey
Although I was able to complete this exercise, I am not clear as to what ( randint(0, len(board_in) - 1)) this is doing.

Could you please explain as to how this is working.

randint generates a random integer.

board_in is the board. The length of the board give us the number of columns.


Just Do as like this, in the img above. Hope this works.

1 Like

Call each function on board . What does that mean?

call the function with board as argument seems like the most logical explanation.

Hey
I understand the rational behind the first parts of: randint(0, len(board_in) - 1), up until the (board_in) section. However, I am a little confused by the reasoning behind the: " - 1 " being added at the end.
Could someone please elaborate on that “- 1”?

2 Likes

length starts counting at one, while indexes start counting at zero. If we don’t compensate for this, we could generate an index which is out of bounds.

3 Likes

Ah, makes sense. thank you!

So I copied code form “Get stuck” and it is missleading:

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

both random_row AND random_col retunrs random int in range of size of ROW. Trying to make rectangle like 10x2 will probably result with random_col out of range.

Can someone get a code how to get sieze of the list in list (this is what a bord is; 5 list and 5 string in every of those 5 list)

It should be something like len(board[1])