Python入门之"烤地瓜"练习"
#烤地瓜
class sweetPotato:
#初始化数据
def __init__(self):
self.cookedString = "生的"
self.cookedLevel = 0
self.condiments = []
#第二种输出方式
def __str__(self):
return "地瓜%s(烤了%d0分钟),放了%s"%(self.cookedString,self.cookedLevel,str(self.condiments))
def cook(self,cookedTime):
self.cookedLevel+=cookedTime
if self.cookedLevel>=0 and self.cookedLevel<3:
self.cookedString="生的"
elif self.cookedLevel>=3 and self.cookedLevel<5:
self.cookedString="半生不熟"
elif self.cookedLevel>=5 and self.cookedLevel<8:
self.cookedString="熟了"
elif self.cookedLevel>=8:
self.cookedString="烤糊了"
def addCondiments(self,item):
self.condiments.append(item)
diGua = sweetPotato()
diGua.cook(1)
diGua.cook(1)
diGua.addCondiments("葱花")
print(diGua)
diGua.cook(1)
diGua.addCondiments("洋葱")
print(diGua)
diGua.cook(1)
diGua.addCondiments("鸡蛋")
print(diGua)
diGua.cook(1)
diGua.cook(1)
diGua.addCondiments("辣椒")
print(diGua)
diGua.cook(1)
diGua.cook(1)
diGua.cook(1)
print(diGua)
#第一种输出方式
#print(diGua.cookedString)
运行结果:
地瓜生的(烤了20分钟),放了['葱花']
地瓜半生不熟(烤了30分钟),放了['葱花', '洋葱']
地瓜半生不熟(烤了40分钟),放了['葱花', '洋葱', '鸡蛋']
地瓜熟了(烤了60分钟),放了['葱花', '洋葱', '鸡蛋', '辣椒']
地瓜烤糊了(烤了90分钟),放了['葱花', '洋葱', '鸡蛋', '辣椒']