2015-10-15 28 views
4

我得到这个错误,但是我选择缩进它,我仍然得到它,你知道为什么吗?Python,PEP-8,E122延续线缺少缩进或缩进

if len(argmaxcomp) == 1: 
    print "The complex with the greatest mean abundance is: {0}"\ 
    .format(argmaxcomp[0]) 
+2

[MCVE](http://stackoverflow.com/help/mcve)请 – sam

+0

适合我。 – m00am

回答

0

one section in the PEP8读取:

缠绕长行的优选方式是通过使用括号内,括号和大括号Python的暗示线延续。通过在圆括号中包装表达式,可以将多条线分成多行。这些应该优先使用反斜杠进行续行。

反斜杠有时可能仍然适用。例如,长,多与-statements不能使用隐式的延续,所以反斜杠是可以接受的

这意味着(尽管这已经无关PEP8-E122),你应该把它包在paranthesis而不是使用的反斜杠然后隐式续行(缩进)是开口支架:

if len(argmaxcomp) == 1: 
    print("The complex with the greatest mean abundance is: {0}" 
      .format(argmaxcomp[0])) 
#   ^--------- The bracket opens here 

只有2中提及的例外,其中反斜杠是可接受的,因为paranthesis是不可能的(因为它们具有在这些上下文另外的含义):

  • with
  • assert小号

但是,如果你真的是反斜杠(只能使用python2)应该具有相同的缩进第一个表达式:

if len(argmaxcomp) == 1: 
    print "The complex with the greatest mean abundance is: {0}" \ 
      .format(argmaxcomp[0]) 
#   ^--------- The first expression starts here 
0

在这种情况下的问题是,根本没有缩进 ,显然错误发生在最后一行。 如果括号不只是增加缩进以下选项:

if len(argmaxcomp) == 1: 
    print "The complex with the greatest mean abundance is: {0}" \ 
     .format(argmaxcomp[0]) 

任何空间工程量,但我不知道什么是首选。

0

余did't得到上面的错误,但我已经尝试下面的类型, 请张贴错误,这样我们就可以检查

In [6]: argmaxcomp = [100] 

In [7]: if len(argmaxcomp) == 1: 
    ...:  print 'val: {0}'\ 
    ...:  .format(argmaxcomp[0]) 
    ...:  
val: 100 

In [8]: if len(argmaxcomp) == 1: 
    ...:  print 'val: {0}'.format(argmaxcomp[0]) 
    ...:  
val: 100 

In [9]: if len(argmaxcomp) == 1: 
    ...:  print 'val: {0}'.format(
    ...:  argmaxcomp[0]) 
    ...:  
val: 100 
4

一般PEP8建议你prefer parenthesis over continuation lines

包装长行的首选方式是使用Python在括号,括号和大括号中的隐含行连续。通过在圆括号中包装表达式,可以将多条线分成多行。这些应该优先使用反斜杠进行续行。

即:

if len(argmaxcomp) == 1: 
    print("The complex with the greatest mean abundance is: {0}" 
      .format(argmaxcomp[0])) 

另一种选择,就是使用Python 3的打印:

from __future__ import print_function 

if len(argmaxcomp) == 1: 
    print("The complex with the greatest mean abundance is:", argmaxcomp[0]) 

注:print_function可能会破坏/需要更新代码的其余部分...你在任何地方使用过打印。

0

刚刚有一个类似的问题,并解决它。我认为OP代码的问题在于延续线之间可能有空白。 \ n除了\ n应该没有任何东西。