Hide... (I need further explanations)


#1



I stopped when the Battleship course asked me to test my code before going any further and I realize that I sometimes can get a code to work (actually to get accepted by CC Console) without understanding what it really do.

So for example, in the Hide... lesson, we're generating random coordinates for our ship to be hidden in. But I still can't understand the idea of substracting 1 from the length of the board.


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

Why do we need to substract 1 ?
Also, is there any reason for feeding board as a parameter since we're going to call it later anyway ?
I understand that we can feed a variable as a parameter for a function if we're gonna print it right inside the function (as in the print_board function) but not in this example.


#2

If a function is to select a random coordinate for a board, then it will need a reference to that board to look at its size.

Your two functions have exactly the same code, but should have different behavior. That's a problem.

As for why you have to subtract 1 that's a matter of considering what len returns and what arguments randint accepts and what your function is supposed to do.
For example, if there are two peanuts in each pod, then in order to write a program that computes the number of peanuts contained by X pods, you would have to consider the relationship between the number of pods and peanuts and then apply the appropriate operation to get your desired result. You would multiply by 2.


#3

Run this code in IDLE and change the -1 to 1 see what happens remove it then run it again, after that use +1.Then do the same thing in the codecademy environment This might help you figure out your code as you work along. I run all my code in IDLE before i submit it. It helps me, it might help you too. Also i will copy and past lines of code into goggle to read about its structure and purpose.

if you remove the -1 u get this
Oops, try again. It looks like your row bounds are not correct.

The -1 prevents the random number generator from generating a number outside the index length of the variable BOARD.


#4

Thank you both, you got the information through my head.

@ionatan , You're right on two things :
First, I had two functions doing the same thing. Useless. I corrected that and assigned each block to its respective variable :wink:
In your example, I got the basic idea but very vaguely. I still got work to do to understand the maths behind concluding that you need to multiply by 2.

@djm001, I read a lot about IDLE and you're right it seems that I need it since I'm still learning the basic works of Python's paradigms. I started to use it.
Thanks to you, while tweaking the randint arguments, it hit me. As @ionatan n hinted that I need to see what kind of arguments does randint accept, in IDLE I discovered that randint doesn't care about the index value of an element, only the range of a given variable. (A little bit like the range function).

Thanks a lot. [ :


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.