|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
class HotDog:
def __init__(self):
self.cooked_level = 0
self.cooked_string = 'Raw'
self.condiments = []
def __str__(self):
msg = 'hot dog'
if len(self.condiments) >0:
msg = msg + ' with '
for i in self.condiments:
msg = msg+ i +','
msg = msg.strip(',')
msg = self.cooked_string + ' ' + msg + '.'
return msg
def cook(self,time):
self.cooked_levevl = self.cooked_level + time
if self.cooked_level >8:
self.cooked_string = 'Charcoal'
elif self.cooked_level >5:
self.cooked_string = 'Well-done'
elif self.cooked_level >3:
self.cooked_string = 'Medium'
else:
self.cooked_string = 'Raw'
def addCondiment(self,condiment):
self.condiments.append(condiment)
myDog = HotDog()
print(myDog)
print('Cooking hot dog for 4 minutes...')
myDog.cook(4)
print(myDog)
print('Cooking hot dog for 3 more minutes...')
myDog.cook(3)
print(myDog)
print('What happens if I cook it for 10 more minutes?')
myDog.cook(10)
print(myDog)
print('Now,I\'m going to add some stuff on my hot dog')
myDog.addCondiment('ketchuo')
myDog.addCondiment('mustard')
print(myDog)
上面代码运行之后为什么是下面这样的:
Raw hot dog.
Cooking hot dog for 4 minutes...
Raw hot dog.
Cooking hot dog for 3 more minutes...
Raw hot dog.
What happens if I cook it for 10 more minutes?
Raw hot dog.
Now,I'm going to add some stuff on my hot dog
Raw hot dog with ketchuo,mustard.
不应该是下面这样吗?
Raw hot dog.
Cooking hot dog for 4 minutes...
Medium hot dog.
Cooking hot dog for 3 more minutes...
Well-done hot dog.
What happens if I cook it for 10 more minutes?
Charcoal hot dog.
Now,I'm going to add some stuff on my hot dog
Charcoal hot dog with ketchuo,mustard.
最简单的错误:拼写错误。
def cook(self,time):
self.cooked_levevl = self.cooked_level + time
|
|