代码如下:
class Dog():
def init(self, name, age):
self.name = name
self.age = age
def sit(self):
print(self.name.title() + "is now sitting.")
def roll_over(self):
print(self.name.title() + "rolled over!")
my_dog = Dog('willie' ,6)
my_dog.sit()
提示的错误如下:
TypeError Traceback (most recent call last)
<ipython-input-13-769d83419730> in <module>
----> 1 my_dog = Dog('willie' ,6)
2 my_dog.sit()
3 #print("My dog's name is " + my_dog.self.name.title())
4 #print("My dog is "+ str(my_dog.self.age)+ "years old!")
TypeError: object() takes no parameters
这是个天坑,查阅资料发现当定义init方法是左右因该是2个下滑线如__init__()