使用IDLE和if-else块时出现Python缩进错误,在命令行上可以正常工作
我得到以下缩进错误使用IDLE和if-else块时出现Python缩进错误,在命令行上可以正常工作,python,Python,我得到以下缩进错误 >>> number = 35 >>> if number == 35: print 'true' else: File "<pyshell#130>", line 3 else: ^ IndentationError: unindent does not match any outer indentation level >>> >编号=35 >>>如果数字==35
>>> number = 35
>>> if number == 35:
print 'true'
else:
File "<pyshell#130>", line 3
else:
^
IndentationError: unindent does not match any outer indentation level
>>>
>编号=35
>>>如果数字==35:
打印“真”
其他:
文件“”,第3行
其他:
^
缩进错误:未缩进与任何外部缩进级别不匹配
>>>
这只在空闲时发生,不在命令行上发生。我正在Windows XP上使用Python 2.6.4。
我在网上搜索过,但没有找到任何关于我为什么会出现这个错误的答案。谢谢你的帮助
另外,在复制和粘贴代码时,代码可能不会正确缩进。但它在空闲界面上正确缩进。您应该手动定义缩进
>>> number = 35
>>> if number == 35:
... print 'true'
... else:
... pass
...
true
任何代码块(在:
之后开始)的缩进应至少比父代码块多一个空格(或制表符)。在您的示例中:
if number == 35:
print 'true'
else:
...
但不是:
if number == 35:
print 'true'
else:
...
正确手动缩进代码对我来说,这很有效:
>>> if number == 35:
....print 'true'
else:
....print 'false'
真的
看起来“>>>”未计入缩进。我使用的是2.7.13可能不是您想要的方式,但它显然简单有效: 而不是这个:
>>> number = 35
>>> if number == 35:
print 'true'
else:
如果关键字如下,则始终可以在python idle上检查条件,而无需使用:
>>> number = 35
>>> number == 35
True
正确,不要缩进else语句。我使用的是3.9版
如果数字==35:
打印(“真实”)
其他:
打印(“假”)
检查是否混合了制表符和空格。这显然是Windows中IDLE中的一个错误,OP-我有一个文件没有dedent,没有制表符,所有缩进都完全正确(4个空格),我偶尔会遇到这个错误。文件在Wing和命令行上的运行与预期完全一致:类似地,IDLE通常会破坏文件->打开并任意复制文件的一部分。不要使用IDLE,这是我的基本观点:它是准IDEs的windows8。
>>> number = 35
>>> number == 35
True