Danger, Will Robinson! Strange "None"


This is my code:

from random import randint

board = []

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

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


def random_row(board):
  return randint(0, len(board) - 1)

def random_col(board):
  return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)
print ship_row
print ship_col

guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
  print "Congratulations! You sank my battleship!"
  print "You missed my battleship"
  board[guess_row][guess_col] = "X"
  print print_board(board)

When I run it it works fine, but prints “None” at the last line, below the “board”. Is it normal (I don’t beleive) and if not, what is wrong?

Hi, @script4494162026

Next time if you have code you need help with, please remember to use this button to format it for the forums:
It makes it easier to read since it includes all your formatting :slight_smile:

As for your code, notice that your print_board() function does not have a return statement. It only prints the board, but does not return it.

Thus, when you write this line:

you are telling your computer to run the print_board() function (which prints the board to the terminal) and print what print_board() returns. Because print_board() has no return statement, it returns nothing. Try calling your function instead of printing it and see what happens.

Happy coding!


Thank you for your quick and helpful responde. Yes, you are right, that was the problem.

Also appreciate the hint for the formatin. I was wondering how to do it.

No problem! Don’t forget to mark my response as the solution if it helped you solve your problem.

1 Like

I have edited your original post to fix the formatting. :slight_smile:

1 Like