- 运行如下程序出现TypeError: Dog() takes no arguments
class Dog:
"""一次模拟小狗的简单尝试"""
def _init_(self,name,age):
"""初始化属性name和age"""
self.name=name
self.age=age
def sit(self):
"""模拟小狗收到命令时蹲下。"""
print(f"{self.name}is now sitting.")
def roll_over(self):
"""模拟小狗收到命令时蹲下。"""
print (f"{self.name}rolled over!")
my_dog=Dog('Willie',6)
print(f"My dog's name is{my_dog.name}.")
print(f"My dog is {my_dog.age} years old.")
2.问题所在
def _init_(self,name,age):##这里的下划线出现了错误
3.解决方法
def __init__(self,name,age):##这里的下划线要敲两遍