已解决(Python代码缩进问题)IndentationError: unindent does not match any outer indentation level

已解决(Python代码缩进问题)IndentationError: unindent does not match any outer indentation level







报错代码


粉丝群一个小伙伴提出的问题:

age = 10
if age > 1:
    print("年龄大于1")
else:
    print("年龄小于1")
 print("我的年龄是" % age)

可以看到代码又高亮:


在这里插入图片描述

报错信息

  File "E:/Python学习/2.py", line 6
    print("我的年龄是" % age)
                        ^
IndentationError: unindent does not match any outer indentation level


报错翻译


报错内容翻译:

缩进错误:未缩进与任何外部缩进级别不匹配



报错原因


报错原因:Python语法严格要求代码的缩进问题,没按要求缩进就会报错



解决方法


仔细看代码那多了一个空格:


在这里插入图片描述

修改代码为:

age = 10
if age > 1:
    print("年龄大于1")
else:
    print("年龄小于1")
print("我的年龄是" % age)

Pycharm自动整理代码的方法(强烈推荐)Ctrl+Alt+L

猜你喜欢

转载自blog.csdn.net/yuan2019035055/article/details/126023803