How do I print each row of the board?


#1

Question

How do I print each row of the board?

Answer

Recall that a for loop can take a list like board and the variable becomes the next item in that list every time it loops. So if we write for item in list_name:, item becomes each item in the list called list_name.
Then inside the for loop we simply print that item by doing print item.
With our board, we’d like to print each row, so by filling in those spots in the for loop template, we should get something along the lines of:

def print_function(list_input):
  for item in list_input:
    print item

print_function(my_list)

At the end, be sure to call our new function and pass it the board!


FAQ: Learn Python - Battleship - Custom Print