如何在类中修复TypeError

时间:2019-08-21 11:47:56

标签: python

我尝试运行此代码,但我不断收到此错误。我不知道是什么导致错误。请有人可以帮我吗?

class Dog():
    """ A simple attempt to describe a dog"""
def __init__(self, name, age):
    """ Initialize name and age attributes."""
    self.name = name
    self.age = age
def sit(self):
    """ Simulates a dog sitting in response to a command."""
    print(f"{self.name}.title() " + "is now sitting.")
def roll_over(self):
    """ Simulates a dog rolling over in response to a command."""
    print(f"{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.")

错误

[Running] python -u "/Volumes/Macintosh HD2/python_project/python_work/classes.py"
Traceback (most recent call last):
  File "/Volumes/Macintosh HD2/python_project/python_work/classes.py", line 14, in <module>
    my_dog = Dog('willie', 6)
TypeError: Dog() takes no arguments

[Done] exited with code=1 in 0.055 seconds

2 个答案:

答案 0 :(得分:1)

很简单-您在 let mainQuestions= questions; let localQuestions = questions; mainQuestions["q-01"].title = "a"; 之后没有缩进任何内容。 缩进它,一切都应该没事。

class Dog()

答案 1 :(得分:0)

在python中,缩进很重要

set ::env(PYTHONPATH) {C:\Python\3.6\wherever;C:\Users\me\Python\3.6\wherever} 该代码没有缩进,有在线缩进检查器,以防您遇到困难。