How do I print each row of the board?

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!

4 Likes

Is there a way that the function will not print β€œNone” at the end ?

The function is not doing the printing of None, functions return None in case of absence of a return keyword, if you then decide to print the returned result, you indeed get None printed

but without any code we get to see, that is the most logic guess we can do

1 Like

I got the same result in my terminal, which logically, is how I got here in the first place. It sounds like, according to stetim94, and since the project is letting me continue, this is not presenting a real problem at the current juncture in the program. For clarity and thoroughness, I just wanted to include a sample of my code, which I assume is probably similar to cassepipe’s.

#Here’s my code:
board =
for i in range(0, 5):
board.append([β€œO”] * 5)
def print_board(board_in):
for row in board:
print(row)
print(print_board(board))

#this printed:
[β€˜O’, β€˜O’, β€˜O’, β€˜O’, β€˜O’]
[β€˜O’, β€˜O’, β€˜O’, β€˜O’, β€˜O’]
[β€˜O’, β€˜O’, β€˜O’, β€˜O’, β€˜O’]
[β€˜O’, β€˜O’, β€˜O’, β€˜O’, β€˜O’]
[β€˜O’, β€˜O’, β€˜O’, β€˜O’, β€˜O’]
None

the print_board function does not return anything, so why would you want to print the returned value, which you do here:

print(print_board(board))

If we define the print_board function like so then call it with board…:

def print_board(board_in):
  for row in board:
    print row #here, it's already handling the **print**ing
    
print_board(board)

Printed result:

['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']
['O', 'O', 'O', 'O', 'O']

why is it printing β€œO” infinite times??
my code:

def print_board(board_in):
  for row in board:
    print row #here, it's already handling the **print**ing
    
print_board(board)

i tried this code:

board = []

for i in range(5):
  board.append(["O"] * 5)

def print_board(board_in):
  for row in board:
    print row #here, it's already handling the **print**ing
    
print_board(board)

in this exercise, and the board is printed two times.

i kept printing the 5 rows. but it would print twice (10 rows total)

all i had to to was return instead of print

still don’t get loops through. just guessing

what is your question? Please also include code samples and any possible error messages

I have the same issue, my board was printed twice (so 10 rows of 5 β€œO’s”.) The exercise still counted my answer as correct, however.
Here is my code:

board =
for x in range(0, 5):
board.append([β€œO”, β€œO”, β€œO”, β€œO”, β€œO”])

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

print_board(board)

what if you run the code on repl.it? The additional output might be caused by the exercise validation.