错误:IndentationError:意外缩进

时间:2019-06-26 00:18:30

标签: python-3.x python-3.7

我是python的新手,因此在基于类和对象的问题上运行代码时遇到此错误:IndentationError:意外缩进 但不知道我哪里出了问题 请帮我得到它以及如何解决它 感谢你在期待.... 祝你今天愉快! :>

class Get_started:
    def __init__(self,name):
        self.name = str(input('Enter name'))
        print('Hello ',self.name)
        self.rect = self.Rectangle()

    class Rectangle:
    def __init__(self):
        print('In this program we are gonna calculate the area of rectange')

    def get_lb(self,l,b):
        self.l = int(input('Enter length  : '))
        self.b = int(input('Enter breadth : '))

    def show():
        print('Length & Breadth of rectangle are ',self.l,' & ',self.b)
    def cal_area(self):
        self.area = self.l * self.b
            print('Area : ',self.area)

m1 = Get_started('mr_halkat')
m1.Rectangle.get_lb()
m1.Rectangle.show()
m1.Rectangle.cal_area()

2 个答案:

答案 0 :(得分:0)

您需要缩进Rectangle类中的所有内容,并减少print方法中cal_area语句上的缩进。

这是修改后的代码:

#!/usr/bin/python

class Get_started:
    def __init__(self,name):
        self.name = str(input('Enter name'))
        print('Hello ',self.name)
        self.rect = self.Rectangle()

    class Rectangle:
        def __init__(self):
            print('In this program we are gonna calculate the area of rectange')

        def get_lb(self,l,b):
            self.l = int(input('Enter length  : '))
            self.b = int(input('Enter breadth : '))

        def show():
            print('Length & Breadth of rectangle are ',self.l,' & ',self.b)
        def cal_area(self):
            self.area = self.l * self.b
            print('Area : ',self.area)

m1 = Get_started('mr_halkat')
m1.Rectangle.get_lb()
m1.Rectangle.show()
m1.Rectangle.cal_area()

答案 1 :(得分:0)

它说class Rectangle中没有函数,并且cal_area(self)函数中的缩进错误

您是否尝试像这样放置代码:

class Rectangle:
     def __init__(self):
          print('In this program we are gonna calculate the area of rectange')
     def get_lb(self,l,b):
          self.l = int(input('Enter length  : '))
          self.b = int(input('Enter breadth : '))

     def show():
          print('Length & Breadth of rectangle are ',self.l,' & ',self.b)
     def cal_area(self):
          self.area = self.l * self.b
          print('Area : ',self.area)