Python入门之"烤地瓜"练习

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分钟),放了['葱花', '洋葱', '鸡蛋', '辣椒']
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值