类中的Python错误
下面是测试用例,之后是我的代码 我需要的是纠正我的错误,自我教育。在下面的测试用例中,当正确的测试用例是“喵喵喵喵喵喵喵喵喵喵喵喵呜呜”时,我的代码会声明“喵喵喵喵说喵喵呜呜”。 其他测试用例是正确的类中的Python错误,python,Python,下面是测试用例,之后是我的代码 我需要的是纠正我的错误,自我教育。在下面的测试用例中,当正确的测试用例是“喵喵喵喵喵喵喵喵喵喵喵喵呜呜”时,我的代码会声明“喵喵喵喵说喵喵呜呜”。 其他测试用例是正确的 #test cases meow_meow = Tamagotchi("meow meow") meow_meow.teach("meow") meow_meow.play() >>>>"meow meow says meow" meow_meow.tea
#test cases
meow_meow = Tamagotchi("meow meow")
meow_meow.teach("meow")
meow_meow.play()
>>>>"meow meow says meow"
meow_meow.teach("purr")
meow_meow.teach("meow")
meow_meow.play()
>>>>'meow meow says meow and purr' #My own code state " meow meow says meowpurr"
使用我的代码:
class Tamagotchi(object):
def __init__(self, name):
self.name = name
self.words = str(self.name) + " says "
#check alive, dead within all the methods
self.alive = True
#trouble portion
def teach(self, *words):
if self.alive == False:
self.words = self.name + " is pining for the fjords"
return self.words
else:
listing = []
for word in words:
listing.append(str(word))
B = " and ".join(listing)
self.words += B
def play(self):
return self.words
def kill(self):
if self.alive == True:
self.words = self.name + " is pining for the fjords"
self.alive = False
return self.name + " killed"
else:
return self.name + " is pining for the fjords"
谢谢不要将
单词
存储为字符串;将其存储为列表,并仅在运行.play()
时使用和“
加入列表;在这里,您还可以测试您的tamagotchi是否还活着:
class Tamagotchi(object):
def __init__(self, name):
self.name = name
self.words = []
self.alive = True
def teach(self, *words):
self.words.extend(words)
def kill(self):
self.alive = False
def play(self):
if self.alive:
return '{} says {}'.format(self.name, ' and '.join(self.words))
else:
return '{} is pining for the fjords'.format(self.name)
您的测试用例似乎不需要Tamagotchi.teach()
和Tamagotchi.kill()
来返回任何内容