python 2.7.1中非常基本的代码行上的IndentationError

时间:2013-08-28 02:55:13

标签: python python-2.7

>>> if temp > 60 < 75:
    print 'just right'
else:

文件“”,第3行     其他:         ^ 这是出现的错误---&gt; IndentationError:unindent与任何外部缩进级别都不匹配

当我按下输入时,它就会出现,我不知道如何解决它,对不起,我知道这可能是一个非常愚蠢的问题,但我刚刚开始,因此非常基本的代码和错误。

4 个答案:

答案 0 :(得分:1)

首先,你应该使用:

if temp > 60 and temp < 75:

或:

if 60 < temp < 75:

一旦解决了这个问题,请确保遵循Python指南进行缩进。当您不这样做时(例如混合制表符/空格,使用太少或太多的空格等),通常会发生缩进错误。

根据您发布的内容,缩进看起来不错,但有时很难说。下面的Python 2.7.3会话,使用四个空格进行缩进,工作正常:

>>> temp = 62
>>> if temp > 60 < 75:
...     print "okay"
... else:
...     print "urk"
... 
okay

但是当我(愚蠢地)在else:之前放置一个空格时,我看到,与你类似:

>>> temp = 62
>>> if temp > 60 and temp < 75:
...     print "okay"
...  else:
  File "<stdin>", line 3
    else:
        ^
IndentationError: unindent does not match any outer indentation level

答案 1 :(得分:0)

你需要缩进你的缩进:

if temp > 60 < 75:
    print 'just right'
else:

if是一个条件,下一行(如果if语句为True则完成)应该被隐藏,然后else:应与if语句对齐

答案 2 :(得分:0)

首先,你的if语句需要修复,你需要像这样缩进你的行:

if temp > 60 and temp < 75:
    print 'just right'
else:
    pass # whatever you need to do here

答案 3 :(得分:0)

Python使用冒号(:)和缩进来分组语句,而其他语言使用花括号({})或括号。因此,您需要在if / else语句以及for,while和foreach语句中缩进Python语句块。所以你的代码应该是:

if 60 < temp < 75:
    print 'just right'
else:
    pass # this doesn't execute anything; it's a placeholder

请注意,Python允许您chain inequalities(例如60 < x < 75而不是x > 60 and x < 75),尽管许多其他语言都没有。