【问题标题】:Python is telling me an indention is expected where I put onePython告诉我在我放一个缩进的地方应该有一个缩进
【发布时间】:2011-12-09 03:00:15
【问题描述】:

好的,下面是一段脚本:

def start():  
    print "While exploring the ruins of a recently abandoned castle you stumble apon the entrance to what appears to be a dungeon. You are carrying on you a...

我不断收到错误

user@ubuntu:~/Documents/python$ python dungeon.py  
  File "dungeon.py", line 533  
    def start():  
    ^  
IndentationError: expected an indented block

我知道这可能很明显,但有人知道我做错了什么吗?我尝试更换缩进 只有空格和制表符,但它仍然给我这个错误。我很感激任何答案。

【问题讨论】:

  • def start(): 之前的行是什么?

标签: python block indentation


【解决方案1】:
  • 确保没有混合制表符和空格来缩进
  • 您的字符串缺少右引号

此外,def start() 实际上是否在您的文件中缩进?这个问题没有缩进,这就是 Python 似乎在抱怨的。

【讨论】:

    【解决方案2】:

    看起来它希望 def start(): 缩进。之前的代码是什么样子的?

    【讨论】:

    • 就是这样,缩进前的那一行。谢谢大家的帮助。
    【解决方案3】:

    检查空格是否一致(即混合制表符和空格?)。

    如果您的编辑器支持,我建议将所有选项卡扩展为 (4) 个空格。这避免了这种混淆,在复制粘贴代码时也是如此。

    在 vim 中:

    :se tabstop=4 shiftwidth=4 expandtab
    :%retab
    

    【讨论】:

      【解决方案4】:

      您确定该位置有tab 字符吗?尝试使用vi 打开它并添加自己的tab 以检查是否正常。

      【讨论】:

        【解决方案5】:

        尝试查看该行之前的代码。是否有未完成的块(以: 结尾的行)?如果是,请在该块中添加一些内容——pass 语句就可以了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-09-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-07-14
          • 1970-01-01
          • 1970-01-01
          • 2018-11-22
          相关资源
          最近更新 更多