I'm trying to improve upon the "battleship" game you make in the codecademy course for python and I decided my first step would be to implement classes.
My expected output from the code is five rows of O O O O O
. This is achieve when I use just a print statement inside of the for loop but it throws an error stating:
Traceback (most recent call last):
File "D:/PythonProjects/Battleship/Main.py", line 21, in <module>
print(board)
TypeError: __str__ returned non-string (type NoneType)
And when I leave the code as is it doesn't throw and errors but only prints one row of O's
Code in question:
class Board(object):
"""Creates the game board"""
board = []
def __init__(self, size):
self.size = size
for x in range(size):
self.board.append(["O"] * size)
def __str__(self):
for row in self.board:
display = (" ".join(row))
return display
board = Board(5)
print(board)