For an assignement for school I need to make a chess game in python. But I'm stuck at a little obstacle.
I want the user to make a chess-piece like this:
p=Pawn(White)
And I want a print to work like this:
print(p) ##Output: White pawn
And in order to get this done I need to use class inheritance, but it doesn't work for me. Here is what I have currently:
WHITE=1
BLACK=2
class ChessPiece:
def __init__(self,color):
self.color=color
def __str__(self):
if self.color==1:
print('Witte',self.name)
else:
print("Zwart ",self.name)
class Pawn(ChessPiece):
def __init__(self):
self.naam='pawn'
self.kleur=kleur
self.naamorself.name. You must choose and use the same everywhere.nameandcoloron both classes, notnaamandkleur.self.__class__.__name__. That gives you the name of the class, here that would bePawn.1,2would be0,1orTrue,False.