__init__()恰好取3个参数(给定1个)

时间:2022-06-01 23:18:35

I just started learning Python today, so I am sorry if this is an easy question. I have spent the last half an hour attempting to rectify the following code.

我今天刚开始学Python,如果这是一个简单的问题,我很抱歉。我花了半个小时试图修改下面的代码。

class Area:

    def __init__(self,width,height):
        self.width = width
        self.height = height

    def rectangle_area(self):
        area = self.width * self.height
        return area

area = Area(200, 100)
print area.rectangle_area()
area = Area(250, 150)
print area.rectangle_area()

Every time I run it, I get outputs 20000 and 37500, which is fine, but then I get:

每次运行时,输出为20000和37500,这没问题,但我得到:

'__init__() takes exactly 3 arguments (1 given)'

Exact output:

准确的输出:

20000
37500
=> None
'__init__() takes exactly 3 arguments (1 given)'

Can anyone see the error in this code?

有人能看到这段代码中的错误吗?

EDIT: This is from problem 3 of http://www.learnstreet.com/assignments/525231f376b99c634f000021/practice/5215e57f76b99c0f52000095

编辑:这来自http://www.learnstreet.com/assignments/525231f376b99c634f000021/practice/5215e57f76b99c0f52000095的问题3

EDIT 2: The question (Copy paste):

编辑2:问题(复制粘贴):

3 : Function init Create a class Area which has a constructor that will assign height as 100 and width as 200. Define a method rectangle_area which should return the area of a rectangle and an instance of Area as area. Print the area of the rectangle. Add 50 to height, and 50 to the width of the area instance. Then print the area of the rectangle.

3:函数init创建一个类区域,该类区域具有一个构造函数,该构造函数将指定高度为100,宽度为200。定义一个方法矩形区域,该区域应该返回矩形区域和区域的实例。打印矩形的面积。高度增加50,区域实例的宽度增加50。然后打印矩形的面积。

EDIT 3: Making them optional gives:

编辑3:使它们可选给予:

20000
37500
=> None
'Have you created two instances named area.height and area.width?'

1 个解决方案

#1


5  

Function __init__: Create a class Area which has a constructor that will assign height as 100 and width as 200. Define a method rectangle_area which should return the area of a rectangle and an instance of Area as area. Print the area of the rectangle. Add 50 to height, and 50 to the width of the area instance. Then print the area of the rectangle.

函数__init__:创建一个具有构造函数的类区域,该构造函数将高度指定为100,宽度指定为200。定义一个方法矩形区域,该区域应该返回矩形区域和区域的实例。打印矩形的面积。高度增加50,区域实例的宽度增加50。然后打印矩形的面积。

To me this sounds like this, as useless as that may seem:

对我来说,这听起来像这样,毫无用处:

class Area:
    def __init__ (self):
        self.width = 200
        self.height = 100
    def rectangle_area (self):
        return self.width * self.height

x = Area()
print(x.rectangle_area())
x.width += 50
x.height += 50
print(x.rectangle_area())

#1


5  

Function __init__: Create a class Area which has a constructor that will assign height as 100 and width as 200. Define a method rectangle_area which should return the area of a rectangle and an instance of Area as area. Print the area of the rectangle. Add 50 to height, and 50 to the width of the area instance. Then print the area of the rectangle.

函数__init__:创建一个具有构造函数的类区域,该构造函数将高度指定为100,宽度指定为200。定义一个方法矩形区域,该区域应该返回矩形区域和区域的实例。打印矩形的面积。高度增加50,区域实例的宽度增加50。然后打印矩形的面积。

To me this sounds like this, as useless as that may seem:

对我来说,这听起来像这样,毫无用处:

class Area:
    def __init__ (self):
        self.width = 200
        self.height = 100
    def rectangle_area (self):
        return self.width * self.height

x = Area()
print(x.rectangle_area())
x.width += 50
x.height += 50
print(x.rectangle_area())