【问题标题】:Python expected an indented block on sublime text 3 [duplicate]Python期望sublime text 3上有一个缩进块[重复]
【发布时间】:2017-02-23 04:07:45
【问题描述】:

这是一个更大代码的示例摘录,我不知道为什么它没有正确识别

代码示例:

if True:
    for i in range(1, 10):
        print(i)
    print("Inside the if")
    print("But outside the loop")

实际代码:

def mkfolder(self):
    path = self.edit_dir.text()
    prefix,fromn,ton,formatn
    if not os.path.isdir(path):
        return self.showerror("Erro de parâmetro", "Caminho para criar as pastas é inválido ou não existe\nCód.: 0052")
    if not self.chk_prefix.isChecked():
        prefix = ""
    else:
        prefix = self.edit_prefix.text()    
    if self.chk_count.isChecked():
        fromn = self.spin_from.getValue()
        ton = self.spin_to.getValue()
        formatn = self.spin_format.getValue()
        if ton <= fromn:
            return self.showerror("Erro de parâmetro", "Não é possível fazer uma contagem regressiva\nCód.: 0010")
        if len(str(abs(ton))) < formatn:
            return self.showerror("Erro de parâmetro", "Quantidade de dígitos é insuficiente\nCód.: 0012")
        strFormat = "{0:0>"+ str(formatn) +"}"
        msg = self.askquestion("Aviso", str(ton - fromn) + " pastas\n Em " + path + "\n De '" + prefix + strFormat.format(fromn) + "'\na '"+ pref+ strFormat.format(ton) +"'\n\nConfirma?")
        if msg==22:

            for i in range(fromn, ton+1):
                os.makedirs(path + "/"+ prefix + strFormat.format(i))           
            self.tomain()
            self.mkclean()

【问题讨论】:

  • 您混合了制表符和空格。为什么if True
  • IntelliJ 有一个快速选项,可以在制表符/空格之间自动转换,甚至是行尾 iirc。
  • @Carcigenicate Sublime 也是如此。

标签: python python-2.7 sublimetext3


【解决方案1】:

缩进不正确,因为您混合了制表符和空格。不要那样做。当你这样做时,Python 不喜欢它。每次你这样做,小狗都会哭

【讨论】:

  • 切换IDE后,我的代码似乎充满了那些,有什么快速的解决方案吗?
  • @Mojimi SublimeText3 应该能够处理这个问题。 stackoverflow.com/questions/22529265/…
  • find-replace。找到一个制表符('\t'),替换为四个空格。或者,在命令行上运行:python -c 'import sys; with open(sys.argv[-1]) as infile: contents = infile.read().replace('\t', ' '); with open(sys.argv[-1], 'w') as outfile: outfile.write(contents) path/to/file
【解决方案2】:

很有可能,您的制表符和空格不一致。一些文本编辑器具有添加间距的特殊“制表符”,而其他编辑器只是将一个制表符转换为多个“空格字符”。我建议研究如何在文本编辑器中显示非打印字符,以查看哪些行是间隔的,哪些是制表符。

【讨论】:

    【解决方案3】:

    这个工具可能会派上用场检查 Python 中的缩进

    https://pythoniter.appspot.com/

    附加:让您的代码看起来更专业

    http://pep8online.com/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-17
      • 2019-08-02
      • 2012-09-19
      • 2018-11-06
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多