我被指派创建一只名为'Molly'age的狗1。我调用方法grow来增加年龄2并打印它。
我知道如何让莫莉跑,但我对如何增加她的年龄感到困惑,它一直在显示None。
任何帮助都会起作用,但最好能解释一下。提前感谢您的帮助。
class Dog():
""" a class representing a dog """
def __init__(self, name, age):
""" Initialize name and age attributes. """
self.name = name
self.age = age
def run(self):
""" Simulate a dog running. """
print(self.name.title() + " is running ...")
def grow(self, year):
""" Update age by adding year. """
self.age += year
molly = Dog("Molly", 1)
print(molly.name + " is " + str(molly.grow(2)))
molly.run()
这个的输出是
Molly is None
Molly is running ...
相关分类