您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python-我收到一个IndentationError。我如何解决它?

Python-我收到一个IndentationError。我如何解决它?

def perm(l):
        # Compute the list of all permutations of l
    if len(l) <= 1:
                  return [l]
    r = []
    for i in range(len(l)):
             s = l[:i] + l[i+1:]
             p = perm(s)
             for x in p:
              r.append(l[i:i+1] + x)
    return r

def perm(l):
    # Compute the list of all permutations of l
    if len(l) <= 1:
        return [l]
    r = []
    for i in range(len(l)):
        s = l[:i] + l[i+1:]
        p = perm(s)
        for x in p:
            r.append(l[i:i+1] + x)
    return r

>>>  print('Hello') # this is indented 
  File "<stdin>", line 1
    print('Hello') # this is indented 
    ^
IndentationError: unexpected indent

>>> age = 10
>>> can_drive = None
>>> 
>>> if age >= 18:
...     print('You can drive')
...      can_drive = True # incorrectly indented
  File "<stdin>", line 3
    can_drive = True # incorrectly indented
    ^
IndentationError: unexpected indent

>>> print('Hello') # simply unindent the line
Hello

>>> age = 10
>>> can_drive = None
>>> 
>>> if age >= 18:
...     print('You can drive')
...     can_drive = True # indent this line at the same level.
... 

>>> if True:
... 
  File "<stdin>", line 2

    ^
IndentationError: expected an indented block

>>> names = ['Sarah', 'lucy', 'michael']
>>> for name in names:
... print(name)
  File "<stdin>", line 2
    print(name)
        ^
IndentationError: expected an indented block

>>> if True:
...     # TODO
...
  File "<stdin>", line 3

    ^
IndentationError: expected an indented block

>>> names = ['Sarah', 'lucy', 'michael']
>>> for name in names:
...     print(name) # The for loop body is Now correctly indented.
... 
Sarah
lucy
michael

def f(arg): pass    # a function that does nothing (yet)

class C: pass       # a class with no methods (yet)

>>> if True:
...     pass # We don't want to define a body.
... 
>>>

>>> if True:
...     if True:
...         print('yes')
...    print()
  File "<stdin>", line 4
    print()
          ^
IndentationError: unindent does not match any outer indentation level

>>> if True:
...     if True:
...         print('yes')
...     print() # indentation level Now matches former statement's level.
... 
yes

>>> 

>>> if True:
...     if True:
...         print()
...     print()
...     print()
  File "<stdin>", line 5
    print()
          ^
TabError: inconsistent use of tabs and spaces in indentation

>>> if True:
...     if True: # tab
...         pass # tab, then 4 spaces
... 
>>>

>>> if True:
...     pass # tab
...     pass # 4 spaces
  File "<stdin>", line 3
    pass # 4 spaces
                ^
IndentationError: unindent does not match any outer indentation level

if True:
    pass
pass # oops! this statement should be indented!.
else:
    pass

Traceback (most recent call last):
  File "python", line 4
    else:
       ^
SyntaxError: invalid Syntax

python 2022/1/1 18:24:16 有626人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶