【问题标题】:How to fix IndentationError: unexpected indent in python script [duplicate]如何修复 IndentationError:python 脚本中的意外缩进 [重复]
【发布时间】:2018-03-26 06:04:12
【问题描述】:

我的 python 代码有问题。我认为我使用了正确的 python 结构,但它一直向我显示(意外的缩进错误)

class TopGamesPostPage(Handler):
    def get(self):
        self.render("topgamespost.html")

    def post(self):
        title = self.request.get('title')
        info = self.request.get('info')

        if title and info: --> unexpected indent error
            p = Post(parent = blog_key(), title = title, info=info)
            p.put()
            self.redirect('/topgames/%s' % str(p.key().id()))
        else:
            error = "subject and content, please!"
            self.render("topgamespost.html", title=title, info=info, error=error)

【问题讨论】:

  • 试试像 Sublime 这样的编辑器。在 Sublime 中,如果你突出显示所有代码,你会看到空格的圆点和制表符的长破折号。如果他们混在一起,那就不行了。 Sublime 还具有转换为所有制表符或所有空格的工具。复制/粘贴代码时经常发生这种情况。
  • 我使用的是 VS 2017,我的文件中没有混合空间

标签: python


【解决方案1】:

由于 Python 期望同一级别的每个缩进行中的空格完全相同,并且空格“长度”在您的编辑器中看起来相同,因此可能是您的文件中混合了制表符和空格.

【讨论】:

    【解决方案2】:

    可能有多种原因,例如混合制表符和空格、隐藏的 unicode 字符等。粘贴code causing problem here 的部分。它会告诉你确切的问题。

    【讨论】:

      猜你喜欢
      • 2020-03-03
      • 2022-06-16
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多