A weird 'None' in a function


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-python/lessons/battleship/exercises/custom-print?action=lesson_resume

<In what way does your code behave incorrectly? Include ALL error messages.>

It works fine. Just a None appear at the end. ‘none’ appears when function returns nothing, but I dont see where the none come from in this case

This is the 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']
None

<What do you expect to happen instead?>

No none should be there

```python
board = []
for i in range(5):
  board.append(['O']*5)
  
 
def print_board(board_in):
  for i in board_in:
    print i

print print_board(board)
<do not remove the three backticks above>

#2

by default, function return None at the end

by adding a print statement in front of the function call, you print the returned result (None)


#3

how do I get rid of none here, please?


#4

so what should we do if we don’t want to print the returned result?


#5

We have to actually return something, so that it will not return ‘none’, and print on a separate line. As the following,

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

print print_board(board)

But this time the loop won’t work, it loops once and return one line:

O O O O O

Where should I fix please?


#6

or you just remove the print statement from the function call so the returned result is not printed


#7

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