chauxvive chauxvive - 2 months ago 7
Python Question

Code academy python "Battleship" - why is "none" printing after my grid?

This is a quickie. The assignment is to build a "Battleship" style grid of 0's. My question is: why is "none" printing after my grid prints? I could easily get through the assignment, but since I'm doing this to learn code I figured I would ask. Thanks!

board = []

for x in range(5):
board.append(["O"] * 5)

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

print_board(board)

Answer

I just did the module for myself on Codecademy. I can see why you got confused, the instructions are pretty confusing.

They really want you to do this:

board = []

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

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

The "---".join(row) was an example showing you how to use join with characters. The instructions on the left side then ask you to use " ".join(row) in the example.

Codecademy automatically prints None at the end of each output for no apparent reason and if you use the " ".join(row) it will be correct with this output:

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