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.

I had the same thing happen. I changed ‘for row in board’ to ‘for row in board_in’ and it then only printed 5 rows instead of 10.

I think the instructions were messed up. By telling us to make the reference to the list itself, which is named “board” (rather than to the argument of the function, which is named “board_in”), I bet it messed something up in the function.

Also, the function could never be passed a different list other than board, using the code as written according to the instructions. The function specifies an argument, but then the argument never gets used again in the function’s definition (because it is mis-written to directly reference the list ‘board’).

I confirmed this:
image

Versus if I fix the function to use the argument, the second list will actually print out:
image

Edit: I just noticed that this time, even with ‘board’ instead of ‘board_in’ it didn’t print 10 rows in a row. Even when I commented out my ‘hi’ list and print, it only printed out 5 rows. Go figure.

If I were to guess it’s because your for loop calls on board, not board_in

Your parameter in the for loop needs to be the board_in, not board, since that’s the one being passed into the function

I created the following function, which produces the right result in the output, but somehow i get a red cross against the action with no explanation and cannot move forward in the lesson:

board = []
for number in range(0,5):
   board.append(["O"]*5)

Up until here it is ok, and below is the function I created whcih generates the right result but isn’t accepted as a correct answer>>>

def print_board(board_in):
  for board_in in range(0,25):
   print board[board_in]

If i call your function:

board = []
for number in range(0,5):
   board.append(["O"]*5)

def print_board(board_in):
  for board_in in range(0,25):
   print board[board_in]

print_board(board)

yes, you see the right result. But the loop is not done yet, which results in an error, which would mean the rest of your program is not going to go well.

I would agree with you. The instructions are calling for use of ‘board’ argument in the FOR loop which is providing desired output in this case. However, it means the whole function ‘print_board’ is not scalable which defies the purpose of it being a function. I’m surprised @codecademy did not correct those instructions so far(?)