5/19 what did "for row in board" do?


#1

Here is the solution. Im having trouble understanding what for statements are really doing and how theyre set up.

My question is for this part

def print_board(board_in):
for row in board:
print row

so we

  1. make a function print_board
  2. give it a paramater board_in
  3. make a for statement. "For row in board"
    So what is “row” in this situation. Is it just one line of board?
  4. print it
  5. we enter the function print_board(board). I wasn’t aware simply writing down a function prints it. I thought we’d have to put “print function_x”

#2

one line, or one row if you prefer. Otherwise, if you don’t include this loop, all the rows will just be printed on a single line:

print board

which doesn’t really look like a battleship board

we call the function print_board, yes, functions need to be called in order to execute

if we want to print the returned result of the function, then yes. Putting a print statement before the function call will print the returned result

inside the function, we have print row, this is what is causing the print.

Beside, this function is very handy for displaying the board after the user made a guess (you will see)


#3

Thank you for the breakdown, my understanding for statements and functions better now.


#4

for loop, print and return are statements. You’re welcome


#5

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