2015-10-15 102 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應該沒有任何東西。