5. Custom print - code correct, but doesn't look right


#1



https://www.codecademy.com/en/courses/python-beginner-en-4XuFm/0/5?curriculum_id=4f89dab3d788890003000096


When I submit the code, it says that it is correct and I can move on. However, it the excercise leads me to believe that it should show up as five O's each on five rows, one above the other. Mine prints out the same as it did before I added the print_board function.


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

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

print board

This is what is showing up:


#2

This is printing the list object in that row. To custom print, we need to put it into string form...

print " ".join(row)

#3

I added 'print print_board(board)' and now it looks correct. I'm not sure if that's what you meant or not, but either way you led me to correct place!


#4

Okay, so you spotted the other issue,

print board

In other words, your function wasn't run in the first place.

print_board()

Now just add the line above to your function in place of print row.


#5

I just want to make sure that I fully understand this before I move on, so I apologize if I am being repetitive.

My code currently reads:

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

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

    print print_board(board)
'''

And the console shows:

After reading your last comment, i changed my code to the following:

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

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

Now the console simply reads 'None', and I am able to move on to the next lesson. But I thought I was supposed to be able to see the board after this step. Is the first block of code correct? I feel like I am missing something.


#6

Definitely not recommended.

def print_board(board)
    for row in board:
        print " ".join(row)

print_board(board)
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

#8

Thank you so much for your help!


#9

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