5.custom print


#1



board = []

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


#3

Hi
I see two problems in your code:

1) the line def print_board(board): should not be indented and should be at the same level as the for

2) In your print board, you print the whole board for each component of board. You should instead print each element board[row]


#4

board = []

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

print_board(board)

this also doesn't work


#5

however this seemed to work, can you please explain this to me, I'm having a hard time understanding it , that would be really kind of you , cheers

board = []

for i in range(5):
board.append(["O"] * 5)
def print_board(board):
for row in range(len(board)):
print board[row]

print_board(board)


#6

Hi
Nice to hear that your problem is solved. You could also have used the code

def print_board(board):
    for row in board:   # <-- for every item in board
        print row       # print the current item, it can be anything

but for your code, we could read it in the following way, also correct

def print_board(board):
        for row in range(len(board)):  # for the number of items in board
            print board[row]           # print the item at index row

#7

board = []
for x in range(5):
---- board.append(['O', 'O', 'O', 'O', 'O'])

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

i am using this code (i also tried what you suggested), the green button and tick appears, but it prints None.
I have continued through section but as long as i write correct code it insists on printing None.
Does anyone knows about that?


#8

Hi
I have two comments :

1) your call to the function print_board with board as a parameter should not be indented.

2) The function print_board should not be called inside a print because the function print_board already prints in the console.

Cheers


Custom Print
#9

Oops. I noticed that i write in last row:
print_board(row)
instead of:
print_board(board)

if i were to use print_board(row), it should be indented because row is not defined for out of loop.

it did work after i fixed it thank you.


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

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

print_board(board)

#11

Hi I just used your second part of the code and it worked. But my first part is different than yours. Here it is:
board = []
for i in range(0,5):
----p = ["O", "O", "O", "O", "O"]
----board.append(p)


#13

Hi, I have a stupid question about the code you gave:

Why is the computer able to print "print_board(board)" without any "print" directive in front of it? I noticed that if I write "print print_board(board)" it will still print the same, so how is it that we can print without writing "print" in the code? I'm just asking because up to this point I've always called the code with "print" in front of what I'm calling, and "print_board()" is the function, not the call to print.