5. Custom Print


#1



I am confused. I have included two codes, the first one gives the desired result (i.e., 5 separate lists of "O").
But in the second code when i replace "print item" with "return item", it doesn't work and gives only one row of "O"

i believe return statement stores the values in the function and print should not have any impact to the function other than just printing out. Therefore i dont understand why using return statement messes up the code and give only one row. can you please clear my mind in this. looks like i got lost


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

board = []
for i in range(0,5):
    board.append(["O"] * 5)
def print_board(board):
    for item in board:      
       return item
print print_board(board)


#2

by default a function returns None at the end, if we want to return something different at the end of the function we can use the return keyword

the catch is that the return keyword will end the function

if the return keyword is reached in a loop, the loop will break so the function can end


#3

@stetim94

ok thanks for that.

in the first code why do i need to have "print item" before print print_board(board). without print item i am not able to get the result.


#4

print item will print each row of your board, print_board(board) is the function cal, executes the function (functions don't execute unless called)


#5

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