Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建一个变量来跟踪python中具有偶数区域的矩形_Python_Class_Oop - Fatal编程技术网

如何创建一个变量来跟踪python中具有偶数区域的矩形

如何创建一个变量来跟踪python中具有偶数区域的矩形,python,class,oop,Python,Class,Oop,我不知道从这里到哪里去。如果使用偶数区域创建矩形,则在调用get_area方法时应打印计算的区域,并且num_矩形的值应增加1。 如果矩形是用奇数区域创建的,则程序应回复一条消息,说明区域不是偶数值 类矩形: 此类表示一个矩形。信息技术 具有长度和宽度,并且能够 计算自己的面积。 定义初始自我,长度=0,宽度=0: 使用可选参数初始化矩形 长度和宽度。 self.length=长度 self.width=宽度 self.num\u矩形=num\u矩形+1 定义报告自我: 返回的字符串表示形式 长

我不知道从这里到哪里去。如果使用偶数区域创建矩形,则在调用get_area方法时应打印计算的区域,并且num_矩形的值应增加1。 如果矩形是用奇数区域创建的,则程序应回复一条消息,说明区域不是偶数值

类矩形: 此类表示一个矩形。信息技术 具有长度和宽度,并且能够 计算自己的面积。 定义初始自我,长度=0,宽度=0: 使用可选参数初始化矩形 长度和宽度。 self.length=长度 self.width=宽度 self.num\u矩形=num\u矩形+1 定义报告自我: 返回的字符串表示形式 长方形 返回长度为:+strself.length的矩形\ +和宽度:+strself.width def get_区域自身: 返回矩形的面积。 self.area=self.width*self.length 如果self.area%2==0: 回程带 r1=矩形2,6 打印r1 打印r1.get_区域 r2=矩形3,5 打印r2 打印r2.get_区域 将num_矩形定义为类变量而不是实例变量,并在get_区域方法中使用if else:


我认为您正在使用python 2.7


检查此问题及其答案

您的代码存在多个问题,无法执行。我到达了你想到达的地方,但首先,你必须理清思路!例如:什么self.num_矩形?变量num_rectangles是什么时候初始化的?我建议重新考虑一下您希望从类中得到什么。在Rectangle类中保留矩形的数量似乎不是一个好主意。也许,您需要一个单独的对象,它将包含所有创建的矩形,并且有一个方法来计算具有奇数区域的矩形的数量?顺便说一句,面积不能保证为整数。num_矩形只能有条件地递增,也就是说,如果长度*宽度%2:Rectangle.num_矩形+=1。作者希望跟踪面积为偶数的矩形的数量。对于不均匀区域,get_area可能不应返回None。@ababak OP希望仅在区域不均匀时打印消息,以便以任何方式进行打印。然而,在return语句中添加了这一点,我认为最初的问题并不是关于Python版本和一般的矩形类。
class Rectangle:
    num_rectangles = 0

    def __init__(self, length = 0, width = 0):

        self.length = length
        self.width = width
        # Rectangle.num_rectangles += 1

    def __repr__(self):
        """
        Returns a string representation of the
        rectangle.
        """
        return "rectangle with length: " + str(self.length) \
            + " and width: " + str(self.width)

    def get_area(self):
        """Returns the rectangle's area."""
        area = self.width * self.length
        if (area %2) == 0:
            Rectangle.num_rectangles += 1
            return str(area) 
        else:
            return "area is not an even value."
class Rectangle:
    __num_rectangles = 0
    """
    This class represents a rectangle. It
    has a length and a width and is capable
    of computing its own area.
    """

    def __init__(self, length = 0, width = 0):
        """
        Initializes a rectangle with an optional
        length and width.
        """
        self.length = length
        self.width = width
        Rectangle.__num_rectangles += 1


    def __repr__(self):
        """
        Returns a string representation of the
        rectangle.
        """
        return "rectangle with length: " + str(self.length) \
            + " and width: " + str(self.width)

    def get_area(self):
        """Returns the rectangle's area."""
        self.area = self.width * self.length
        if (self.area %2) == 0:
            return str(self.area)
        else:
            print("Area isn't an even value")


r1 = Rectangle(2, 6)
print r1
r1.get_area()
print r1.get_area()


r2 = Rectangle(3, 5)
print r2
print r2.get_area()