用于计算矩形和三角形的面积和周长的类 - NameError

时间:2017-11-05 13:39:00

标签: python class testing nameerror

我从这段代码中得到错误,任何想法?看起来我的测试功能有些东西。我真的不明白这一点。我刚刚开始使用类,在类中创建测试函数时,我有什么重要的事情吗?如果我不使用test_Rectangletest_Triangle调用测试函数,代码似乎工作正常。尝试查看我的书和其他示例,但找不到任何解决此问题的方法。

Traceback (most recent call last):
  File "geometric_shapes.py", line 5, in <module>
    class Rectangle(object):
  File "geometric_shapes.py", line 31, in Rectangle
    test_Rectangle()
  File "geometric_shapes.py", line 19, in test_Rectangle
    r = Rectangle(3, 4, 2, 5)
NameError: name 'Rectangle' is not defined

代码:

# Exercise 7.4: Make classes for a rectangle and a triangle

from math import sqrt

class Rectangle(object):
    def __init__(self, x0, y0, w, h):
        self.x0 = x0
        self.y0 = y0
        self.w = w
        self.h = h

    def area(self):
        return self.h*self.w

    def perimeter(self):
        return 2*self.w + 2*self.h

    def test_Rectangle():
        r = Rectangle(3, 4, 2, 5)
        w = 2; h = 5
        expected_area = w*h
        computed_area = r.area()
        diff = abs(expected_area - computed_area)
        tol = 1E-14
        assert diff < tol, "bug in Rectangle.area, diff=%s" % diff

        expected_perimeter = 2*w + 2*h
        computed_perimeter = r.perimeter()
        diff = abs(expected_perimeter - computed_perimeter)
        assert diff < tol, "bug in Rectangle.perimeter, diff=%s" % diff
    test_Rectangle()


class Triangle(object):
    def __init__(self, v1, v2, v3):
        self.v1 = v1
        self.v2 = v2
        self.v3 = v3

    def area(self):
        x1, y1 = self.v1
        x2, y2 = self.v2
        x3, y3 = self.v3
        return 1/2*abs(x2*y3 - x3*y2 - x1*y3 + x3*y1 + x1*y1 - x2*y1)

    def perimeter(self):
        ver = (self.v1, self.v2, self.v3)
        AB = sqrt((ver[1][0] - ver[0][0])**2 + (ver[1][1] - ver[0][1])**2)
        BC = sqrt((ver[2][0] - ver[1][0])**2 + (ver[2][1] - ver[1][1])**2)
        AC = sqrt((ver[2][0] - ver[0][0])**2 + (ver[2][1] - ver[0][1])**2)
        perimeter = AB + BC + AC
        return perimeter

        def test_Triangle():
            t = Triangle((0,0), (1,0), (0,2))
            expected_area = 1
            computed_area = t.area()
            diff = abs(expected_area - computed_area)
            tol = 1E-14
            assert diff < tol, "bug in Triangle.area, diff=%s" % diff

            expected_perimeter = 5.236
            computed_perimeter = t.perimeter()
            diff = abs(expected_perimeter - computed_perimeter)
            assert diff < tol, "bug in Rectangle.perimeter, diff=%s" % diff
        test_Triangle()

1 个答案:

答案 0 :(得分:0)

正如jonsharpe指出的那样,在test_Rectangle

中定义Rectangle函数时,你做错了缩进

以下程序将有效。

修改

之前我认为您从代码中提供了一小部分代码来复制问题,因此无需更改解决方案,但现在已经更新了答案。

<强>更新

因为,测试函数在类中,并且由于没有实例化类,也没有通过这些实例调用测试函数,因此函数没有被调用。

在下面的代码中,我已将您自己的函数移到类定义之外并显式调用它们。

from math import sqrt

class Rectangle(object):
    def __init__(self, x0, y0, w, h):
        self.x0 = x0
        self.y0 = y0
        self.w = w
        self.h = h

    def area(self):
        return self.h*self.w

    def perimeter(self):
        return 2*self.w + 2*self.h



class Triangle(object):
    def __init__(self, v1, v2, v3):
        self.v1 = v1
        self.v2 = v2
        self.v3 = v3

    def area(self):
        x1, y1 = self.v1
        x2, y2 = self.v2
        x3, y3 = self.v3
        return 1/2*abs(x2*y3 - x3*y2 - x1*y3 + x3*y1 + x1*y1 - x2*y1)

    def perimeter(self):
        ver = (self.v1, self.v2, self.v3)
        AB = sqrt((ver[1][0] - ver[0][0])**2 + (ver[1][1] - ver[0][1])**2)
        BC = sqrt((ver[2][0] - ver[1][0])**2 + (ver[2][1] - ver[1][1])**2)
        AC = sqrt((ver[2][0] - ver[0][0])**2 + (ver[2][1] - ver[0][1])**2)
        perimeter = AB + BC + AC
        return perimeter



def test_Rectangle():
    r = Rectangle(3, 4, 2, 5)
    w = 2; h = 5
    expected_area = w*h
    computed_area = r.area()
    diff = abs(expected_area - computed_area)
    tol = 1E-14
    assert diff < tol, "bug in Rectangle.area, diff=%s" % diff

    expected_perimeter = 2*w + 2*h
    computed_perimeter = r.perimeter()
    diff = abs(expected_perimeter - computed_perimeter)
    assert diff < tol, "bug in Rectangle.perimeter, diff=%s" % diff

def test_Triangle():
    t = Triangle((0,0), (1,0), (0,2))
    expected_area = 1
    computed_area = t.area()
    diff = abs(expected_area - computed_area)
    tol = 1E-14
    assert diff < tol, "bug in Triangle.area, diff=%s" % diff

    expected_perimeter = 5.236
    computed_perimeter = t.perimeter()
    diff = abs(expected_perimeter - computed_perimeter)
    assert diff < tol, "bug in Rectangle.perimeter, diff=%s" % diff

test_Triangle()
test_Rectangle()

看看这些python库来编写测试

Unit Test python 2

Unit Test python 3