7/19 - Not understanding why my code is correct


#1

Instructions:
image

It is showing completed, but I never followed that final step of “Call each function on Board.” In fact, I’m not even sure what “calling a function” means in this context.
Here’s what my code looks like:

def random_row(board_in):#random row
  return randint(0, len(board_in) - 1)

def random_col(board_in):#random column
  return randint(0, len(board_in) - 1)

Would "Calling each function on Board" look like this? -

random_row(board)
random_col(board)

EDIT: When I clicked Next, it replaced my code with correct code, which looks like this:

ship_row = random_row(board)
ship_col = random_col(board)

If I didn’t include those two lines, why did it give me the green checkbox?


#2

Typically, you define a function to perform a specific task that you want to re-use multiple times. You “call a function” when you use it to perform that task. For example, if we want to know the length of a string assigned to a variable called some_text, we can call the len function:

the_length = len(some_text)

What we’ve done there is call the function len, passed the variable some_text to it as an argument, and assigned the output from the len function to the variable the_length.

Yes, exactly like that. Those two lines will call both of your functions, and then simply discard the output as you’re not doing anything with it.

I can only guess that it was a glitch in the editor. Sometimes I get the red cross of doom before I’ve even attempted to answer the problem. :slight_smile:


#3

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