Custom print? Help


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

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

print_board(board)

i get always this message: Oops, try again. It looks like you function didn't print out the correct response!
But i checked others in the forum and this code works for them but no for me. why


#2

at this point just do print row


#3

thank you @rydan . it works now !!!!!!!!!!


#4

Thank you. Simple things can me trip sometimes.


#5

check my code, it's much easer to understand

board = []          #empty list
for i in range(0,5):
    board.append(["O"]*5)
    
def print_board(board):
    for row in board:
        print row

print_board(board)

#6

Interesting, where you have -

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

I have -

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


#7

@tucsonvince

I have an example for you,

Example:

def print_board(board):
    print(*("{}\n".format(i) for i in board), sep='')

This niffy little thing use string formatting and a generator expression to create the nice little print out of the board when needed. I should also mention it uses the syntax sugar of unpacking with the generator to the print function to actually work correctly.

The PEP i linked is for 3.5 but you can use that type of unpacking on one item per function call.

I like using string formatting, because it does have it's uses. You can do all types of fun things with it.

I like this example from the string formatting page,

Example: String Formatting

print('{:*^30}'.format('centered'))

As you can see it is quite easy to make some pretty text print outs with this. You can even use it when creating a GUI to make some fancy displays.


#8

I had code that was kind of all over the place but worked against this check in Codecadamy.... I found this website, http://www.pythontutor.com/visualize.html is fantastic for looking at the code unravel and see what calls to what, helped me to remove the BS code that wasn't needed...