Battleship: 7. Hide... Can someone explain this piece of code to me?


#1

Could someone explain this line of code to me, especially the -1 ?

board = randint(0, len(board) - 1)

seems like it means my board is equal to a random integer from 0 to.... length of the board minus 1?
By the look of it I can kind of tell what most of the code is supposed to do, but I cant make sense of that - 1, but if someone could explain the whole thing to me I'd really like to understand this thing further.

saw a similar post to this, but no one fully answered the question.


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):
    board = randint(0, len(board) - 1)
    return board
def random_col(board):
    board = randint(0, len(board) - 1)
    return board
random_row(board)


#2

How does randint use its first and second argument?
What result do you want from randint?
Which arguments do you therefore have to give it?
How can you get that information from the current state of your program?

If I want the sum of 3 and 8, then I'll apply addition to the values 3 and 8. Why did I use 8? Because I wanted the sum of 8, and 3.

If you want a random number between two other, well, which arguments would you then give to a function that has this behaviour?

If I was wondering what addition does at all, then I wouldn't be asking why 8 is being used, I'd be reading about addition. Similarly, if I was wondering about what a function does, I'd go read its documentation


#3

wow...

Yeah, I am aware of the documentation,
random.randint(a, b)
Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1)

I wish i had that mathematically inclined / code ready brain, but I definitely don't, and I learn way better when people explain things. Everyone learns differently.

looking for someone to explain this to me in kind of more human / layman terms.


#4

Well then presumably without -1 it's one too high. Right? So one was subtracted to get the desired upper bound. What other explanation could there be? Where is the confusion?

It could of course also be a bug, perhaps one shouldn't be subtracted. You would just have to consider which bounds you need to that random number and ask for something in the appropriate range.

And if you've read the documentation for the function, then what information are you missing? Seems like you have the answers already. It appears that you're asking either for something you already know, or you're asking for something other than what your question amounts to and would need to rephrase.

There is no reasonable answer to your question. Your question does not add up. You can't expect an answer to it.

Ergo, I explain the problem with your question instead of answering it.


#5

Yikes, I don't know how to explain this without sounding offensive. But, I think I would better understand an answer given by someone else.

I've asked several questions here before and I've gotten clear, less aggressive answers, and I learn better that way.

No offense at all, sometimes an explanation makes sense to one person, while it doesn't to someone else. Thanks for your time though. I appreciate it.


#6

Perhaps they are just giving up and give you the result without ensuring that you can get there yourself, that you will be able to do it for other variations of the very same problem. You then can't do it the next time and ask again, and don't learn the thing that is really preventing you from finding it that they possess. The more of these holes you leave open the more you have to ask. It's a cycle to break.

You need to ask the right questions. You need to identify what stops you from getting the answer.

What I'm seeing is that you just will not consider the problem, and therefore will also not find an answer.

If you have a board with X rows. Then which indexes does it have? What's the lowest one? What's the highest one? How do you compute that from X?

If that is difficult, then use concrete numbers. Put 3 in place of X and see what you get. What upper and lower bounds are there to the indexes, what arguments do you therefore need to a function that selects a random integer in a range?


#7

The function needs to know the highest and lowest value that it should choose between.
The lowest is that of the first index, the highest is that of the last.
Which index is the last? It's the one that is one less than the length of the list.

That's how far you need to go in your thought process. You just need to begin to consider the problem and the answer is right there

The size of a list is enough information to figure out which index is the last one. And that is the information you need to give the function.

Draw it, if all else fails.


#8

Sorry, but this is the last thing I'm gonna say to you.

I asked a simple question:
"Can someone explain this line of code to me. board = randint(0, len(board) - 1) in human-ish words"
A line of code taken from the larger block of code I listed below. which is very possible to do.

If you refuse to do this, then just let someone else answer it. It's an easy thing to do. I've already passed this lesson, I'm just wondering if someone can explain a part that wasn't explained.

Your way of "helping" does not work for me, not only because I personally don't like people who mix accusations and aggressiveness in their explanations, and think it's an effective way to teach. but because this doesn't get me closer to answering my question.
This "tough love" method of teaching is very outdated and primitive, and is terrible for beginners.

You think that if I ask a question like this I'm trying to take the easy way out? That I'm not willing to put in the work to understand this? No, some people are born with mathematically inclined brains, some people are born with brains that are able to grasp other things. I do not grasp code easily. Documentations for any coding language are very hard for me to understand.

These forums are here to help people. There are loads of people on here who are patient and understand that this is difficult for a beginner. You are not one of those people. Please step aside and let those people help.

I've worked in many jobs where I've trained people and have been trained myself. There are people who use the "tough love" method of training, and there are those who patiently explain as much as they can to the trainee. After being trained by both these types I will pick the patient trainer any day of the week.

I really don't care for lengthy internet discussions.
If you want to retaliate with one final useless comment, go ahead, as long as you please let someone else answer this question.


#9

I believe you missed that I explained the reasoning for the -1


#10

In case you're still having trouble:

randint(0, len(board) - 1)

Okay, so lets say we wanted to make a board consisting of a 5x5 grid. So in terms of programming we want to use an array to store our grid. Within that array, we want to store five rows, so that's another five arrays.

Now the most important thing to remember with arrays is that they start from from 0, not 1:

    0   1   2   3   4
0: [o] [o] [o] [o] [o]
1: [o] [o] [o] [o] [o]
2: [o] [o] [o] [o] [o]
3: [o] [o] [o] [o] [o]
4: [o] [o] [o] [o] [o]

So the first row starts at index 0. Likewise, the first "O" we use to represent a single grid square in a row is also stored in index 0.

Now if we want to select a random row, we could simply say "let's select a random integer that represents one of the rows". We know that all arrays start from 0, so our first argument for randint is going to be 0.

What about the second argument? We need a way to calculate the last index in the array. We could simply say that the length of the board is our upper bound and use len(board).

But here's the problem: the length of our array is 5, but the last index is always going to be 1 less than the length of an array. So we could end up with getting a random number that's 5, which would end up being an index that doesn't exist in our case.

Therefore, we need to subtract something from len(board) which ensures that never happens.

Hope this helps!


#11

And instantly that makes sense. Thanks for explaining that.

For some reason while trying to make sense of that argument my brain cast a shadow on those damned index numbers.

Yeah it's the question "why can't we just use len(board) without the -1?" that threw me off. Since the argument started with zero, I just assumed that the randint() function already knew that 4 would be the last index number. It's like "wait, it knows that 0 is the starting point so..."

And that's why whenever I typed number 5 it was out of the ocean? Actually now I'm wondering, choosing column or row 5 in the game should say "out of the ocean" right? I'll be going through all these lessons again, but I'm just wondering.

Thanks again for simplifying that.


#12

Actually now I'm wondering, choosing column or row 5 in the game should say "out of the ocean" right?

It's been a while since I looked at the Battleships course so I can't remember if that's covered or not, but it's definitely a good idea to provide feedback that let's the user know if they are outside the grid :slight_smile:


#13

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