为了学校的任务,我需要用Python下棋。但我遇到了一个小小的障碍。你知道吗
我想让用户做出这样一个棋子:
p=Pawn(White)
我想要一张这样的照片:
print(p) ##Output: White pawn
为了做到这一点,我需要使用类继承,但它不适合我。以下是我目前的情况:
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
这是您的代码的修改版本:
在您的代码中有一些点被忽略了,即
__str__
应该返回一个字符串而不是打印它,并且您应该在继承中调用基类__init__
相关问题 更多 >
编程相关推荐