breaki 发表于 2020-7-20 13:43:16

Python

>>> class juxing:
        def __init__(self,width=0,height=0):
                self.width = width
                self.height = height
        def __setattr__(self,name,value):
                if name == 'square':
                  self.width = value
                  self.height = value
                else:
                  super().__setattr__(name,value)
      def getarea(self):
            return self.width*self.height

SyntaxError: inconsistent use of tabs and spaces in indentation
这是什么错误啊。

zltzlt 发表于 2020-7-20 13:44:23

Tab 与空格混用了,这样试试:

class juxing:
    def __init__(self, width=0, height=0):
      self.width = width
      self.height = height

    def __setattr__(self, name, value):
      if name == 'square':
            self.width = value
            self.height = value
      else:
            super().__setattr__(name, value)

    def getarea(self):
      return self.width * self.height

sunrise085 发表于 2020-7-20 13:46:11

空格和tab混用错误。
python中缩进是格式控制的关键,所以要严格控制缩进
你这个是直接复制粘贴进来的吧?
多行代码请不要直接复制粘贴进来,一般都会出现缩进错误的。
还是老老实实创建一个文件吧

Twilight6 发表于 2020-7-20 13:47:29



去试试这样设置:

Python FAQ 039 Error:Inconsistent indentation detected! 该怎么解决
https://fishc.com.cn/thread-175313-1-1.html
(出处: 鱼C论坛)

breaki 发表于 2020-7-20 13:51:52

zltzlt 发表于 2020-7-20 13:44
Tab 与空格混用了,这样试试:

这串代码中super有什么用啊。

zltzlt 发表于 2020-7-20 13:52:44

breaki 发表于 2020-7-20 13:51
这串代码中super有什么用啊。

调用基类的方法
页: [1]
查看完整版本: Python