You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python出现AttributeError求助:'Project'对象无'rectangle'属性

解决AttributeError:'Project'对象无对应方法的问题

你的代码报错核心原因是:Project类里调用了rectangle()、triangle()、circle()这三个方法,但这些方法实际定义在shape类中,Project既没继承shape,也没创建shape的实例,所以程序找不到这些方法,触发AttributeError。

下面给两种可行的修复方案:

方案一:让Project类继承shape类

直接让Project继承shape,这样Project的实例就能直接使用shape里的所有方法。修改后的完整代码如下:

class Project(shape):

    def __init__(self):
        print("1. Area of rectangle")
        print("2. Area of a triangle")
        print("3. Area of a circle")
        print("4. Quit!")

        while True:
            try:
                selection = int(input("Enter your choice "))

                if selection == 1:
                    self.rectangle()
                elif selection == 2:
                    self.triangle()
                elif selection == 3:
                    self.circle()
                elif selection == 4:
                    break
                else:
                    print("Invalid value. Select 1-4")
            except ValueError:
                 print("Invalid value. Select 1-4. (NO ALPHABETIC LETTERS)")

class shape:

    def rectangle(self):
        base = int(input("Enter the base:"))
        height = int(input("Enter the height"))
        area = height * base
        print(f"The area of a rectangle is {area}")

    def triangle(self):
        base = int(input("Enter the base: "))
        height = int(input("Enter the height: "))
        area = base * height / 2
        print(f"The area of the triangle is {area}")

    def circle(self):
        radius = int(input("Enter the Radius: "))
        area = 3.142 * radius * radius
        print(f"The area of the circle is {area}")

Project()

注:顺便修正了原代码里的两处笔误:把"Area if rectangle"改成"Area of rectangle",把三角形打印语句里的"The are of the triangle"改成"The area of the triangle"

方案二:在Project类中实例化shape对象

如果不想用继承,可以在Project的__init__方法里创建一个shape的实例,然后通过这个实例调用方法。修改后的代码如下:

class Project:

    def __init__(self):
        self.shape_instance = shape()  # 创建shape类的实例
        print("1. Area of rectangle")
        print("2. Area of a triangle")
        print("3. Area of a circle")
        print("4. Quit!")

        while True:
            try:
                selection = int(input("Enter your choice "))

                if selection == 1:
                    self.shape_instance.rectangle()
                elif selection == 2:
                    self.shape_instance.triangle()
                elif selection == 3:
                    self.shape_instance.circle()
                elif selection == 4:
                    break
                else:
                    print("Invalid value. Select 1-4")
            except ValueError:
                 print("Invalid value. Select 1-4. (NO ALPHABETIC LETTERS)")

class shape:

    def rectangle(self):
        base = int(input("Enter the base:"))
        height = int(input("Enter the height"))
        area = height * base
        print(f"The area of a rectangle is {area}")

    def triangle(self):
        base = int(input("Enter the base: "))
        height = int(input("Enter the height: "))
        area = base * height / 2
        print(f"The area of the triangle is {area}")

    def circle(self):
        radius = int(input("Enter the Radius: "))
        area = 3.142 * radius * radius
        print(f"The area of the circle is {area}")

Project()

内容的提问来源于stack exchange,提问作者Huzi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 11:45:35