【发布时间】:2018-10-28 17:31:06
【问题描述】:
您好,我无法弄清楚我在以下代码中做错了什么以产生错误消息,我已经从网络上复制并粘贴了相同的代码,它工作得很好,但是当我输入它时定义的类似乎没有参数。
输入:
class Dog():
"""A simple attempt to model a dog"""
def _init_(self, name, age):
"""initialize name and age attributes."""
self.name = name
self.age = age
def sit(self):
"""simulate dog sitting in response to a command"""
print(self.name.title() + " is now sitting.")
def roll_over(self):
"""simulate rolling over in response to a command"""
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 "C:/Users/sstie/Desktop/python_work/ch.9_retry.py", line 16, in <module>
my_dog = Dog('willie', 6)
TypeError: Dog() takes no arguments
【问题讨论】:
-
_init_需要两个下划线。 -
你的缩进是错误的。
标签: python python-3.x class instance