【问题标题】:Python IndentationError: unindent does not match any outer indentation level [duplicate]Python IndentationError:unindent 不匹配任何外部缩进级别[重复]
【发布时间】:2018-02-16 01:07:27
【问题描述】:

我正在尝试更新 Python 中的字典值。我正在尝试通过从值中减去 3 来更新 dict 中的值。

if buildings == 1:
        workpower -= 3
        if workpower >= 0:
            farmplace1 = int(input("where do you want it?"))
            farmplace2 = int(input("where do you want it?"))
            board[farmplace1][farmplace2] = "F"    
            my_dict['d'] -= 3;

我收到以下错误

IndentationError: unindent does not match any outer indentation level

代码有什么问题?

【问题讨论】:

  • 你的代码应该在每个 if 语句后缩进 4 个空格
  • 编辑模式显示你正在混合制表符和空格,只使用空格

标签: python python-3.x dictionary indentation


【解决方案1】:

检查您是否对所有缩进使用常规空格,最有可能您使用制表符或其他类似的空格字符而不是空格。

混合使用空格字符可能会导致视觉效果很好的缩进,但对解释器不利。

【讨论】:

    【解决方案2】:

    更改缩进并确保其一致(仅使用空格或仅使用制表符,不要混合使用两者):

    if buildings == 1:
        workpower -= 3
        if workpower >= 0:
            farmplace1 = int(input("where do you want it?"))
            farmplace2 = int(input("where do you want it?"))
            board[farmplace1][farmplace2] = "F"    
            my_dict['d'] -= 3
    

    【讨论】:

    • 它是自动化的,标签的东西不知道谢谢
    • 每个 if 语句后面应该有 4 个空格(或一个制表符)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    • 2021-09-30
    • 1970-01-01
    • 2012-07-04
    • 2010-12-15
    • 2020-08-14
    • 1970-01-01
    相关资源
    最近更新 更多