我被要求创建一个python程序,其中包含一个类Fleur(flower)和一个类Bouquet,每个flower类包含一个名称、价格和其他参数,Bouquet类包含flower类中以列表形式显示的对象,我对如何将花添加到花束列表并操纵该列表有问题。多谢各位
class Fleur :
def __init__(self, name ,couleur, prix _base, parfum_=False, promotion=False):
self. n=name
self .c=couleur
self. px=prix _base
self.pa=parfum_
self.pr=promotion
def prix(self):
if self.pr==True:
return self. px*0.5
else:
return self. px
def parfum(self):
return self.pa
def affiche(self):
if self.pa==True :
a=self.n,self.c,'parfumée , prix:', self .prix(),'MAD'
print(a)
else:
a=self .n ,self .c,', prix:', self .prix(),'MAD'
print(a)
@staticmethod
def equal(A,B):
if A. n==B .n and A. c==B .c and A.pa==B.pa:
return True
else:
return False
class Bouquet:
def __init__(self):
self .bouquet=[]
def ajouter(self, f):
self .bouquet. append(f. affiche())
我就走到这里了
在类
Bouquet
的方法ajouter
中,看起来您在追加f.affiche()
,但是affiche()
没有返回任何内容。方法affiche打印输出a,使其返回的值为None
。如果希望能够为给定的f追加affiche的输出,则需要在affiche中返回a,或者只返回设置为a的值的内容代码可能是这样的
相关问题 更多 >
编程相关推荐