注册 登录
编程论坛 Python论坛

照书本抄的代码,又出现问题了。

greenpop 发布于 2019-11-08 15:06, 1406 次点击
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)

print("My dog's name is "+my_dog.name.title()+".")
print("My dog is "+str(my_dog.age)+" years old.")
           
错误信息:
Traceback (most recent call last):
  File "D:\zwg\pythonst\dog.py", line 13, in <module>
    my_dog=Dog('willie',6)
TypeError: Dog() takes no arguments
1 回复
#2
greenpop2019-11-08 15:14
是我init前后的下划线,各少打了一个。
1