如何在课堂上修正TypeError

2024-04-25 08:04:18 发布

您现在位置: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

Tags: tonameinselfagetitleismy
2条回答

在python中,缩进很重要

class Dog():, my_dog = Dog('willie', 6)此代码缺少缩进,有在线缩进检查器,以防您发现困难。你知道吗

很简单-在class Dog()之后没有缩进任何内容。 缩进它,一切都会好起来的。你知道吗

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.")

相关问题 更多 >