我正在从Eric Matthes题为“ Python Crash Course”的书中学习Python。
我正在创建我的第一堂课,并使用书中的代码。但是,当我运行代码时,它给我一个错误,“ object()不带参数。”
使用Sublime Text-Python 2.7
我已经对本书中的代码进行了三遍检查,我确定我正确地获得了它。我不确定我还能尝试什么。
```
class Dog(object):
"""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 a dog sitting in response to a command."""
print(self.name.title() + " is now sitting.")
def roll_over(self):
"""Simulate rollign 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.")
```
该代码的输出应为: 我的狗叫威利。 我的狗6岁了。
相反,我在Sublime Text中收到此错误消息:
"Traceback (most recent call last):
File "/Users/recklessfire13/Library/Application Support/Sublime Text 3/Packages/User/python_work/dog.py", line 19, in <module>
self.my_dog = Dog('willie', 6)
TypeError: object() takes no parameters
[Finished in 0.1s with exit code 1]"
答案 0 :(得分:1)
替换
def _init_(self, name, age):
使用
def __init__(self, name, age):