Python:Classes:Turtle:addFrog()接受1个位置参数,但给出了2个

2024-10-03 02:38:11 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我写的代码 我试图在空闲状态下运行的是:

 >>> mike = Frog('Mike', 'yell')
 >>> course = RaceCourse()
 >>> course.addFrog(julie)
TypeError: addFrog() takes 1 positional argument but 2 were given

我对此特定部件的代码:

^{pr2}$

Tags: 代码状态argument空闲mikecoursetakestypeerror
2条回答

在Python中,当您使用类方法的对象调用它时,它会自动将self作为参数传递。在

在您的例子中,调用时提供了一个变量“julie”。在

所以你必须准备好函数来接受第二个参数 比如:

def addFrog(self, julie):
    # body

得到TypeError的原因是addFrog()函数只定义为接受1参数。在

以下是修复的函数:

def addFrog(self, frog):
    self.turtle.pu()
    spacing = 350/(Frog.lastBib+1)
    Frog.height -= spacing
    self.turtle.goto(-250, Frog.height)

相关问题 更多 >