2015-11-01 22 views
15

我使用vim編輯器的Python IDE,下面是一個簡單的Python程序計算的平方根: -預計兩個空行PEP8警告在python

import cmath 
def sqrt(): 
    try: 
     num = int(input("Enter the number : ")) 
     if num >= 0: 
      main(num) 
     else: 
      complex(num) 
    except: 
     print("OOPS..!!Something went wrong, try again") 
     sqrt() 
    return 

def main(num): 
    squareRoot = num**(1/2) 
    print("The square Root of ", num, " is ", squareRoot) 
    return 

def complex(num): 
    ans = cmath.sqrt(num) 
    print("The Square root if ", num, " is ", ans) 
    return 

sqrt() 

和警告是: -

1-square-root.py|2 col 1 C| E302 expected 2 blank lines, found 0 [pep8] 
1-square-root.py|15 col 1 C| E302 expected 2 blank lines, found 1 [pep8] 
1-square-root.py|21 col 1 C| E302 expected 2 blank lines, found 0 [pep8] 

請問您爲什麼會發出這些警告?

enter image description here

+1

https://www.python.org/dev/peps/pep-0008/#blank-lines – Jasper

回答

23
import cmath 


def sqrt(): 
    try: 
     num = int(input("Enter the number : ")) 
     if num >= 0: 
      main(num) 
     else: 
      complex_num(num) 
    except: 
     print("OOPS..!!Something went wrong, try again") 
     sqrt() 
    return 


def main(num): 
    square_root = num**(1/2) 
    print("The square Root of ", num, " is ", square_root) 
    return 


def complex_num(num): 
    ans = cmath.sqrt(num) 
    print("The Square root if ", num, " is ", ans) 
    return 

sqrt() 

以前將解決您的PEP8問題。導入之後,您需要在開始代碼之前添加兩行新行。此外,每個def foo()之間還需要2個。

在你的情況下,你導入後有0,並且你在每個函數之間有1個換行符。作爲PEP8的一部分,您需要在代碼結束後換行。不幸的是,當我將代碼粘貼到此處時,我不知道如何顯示它。

注意命名,它也是PEP8的一部分。我將complex更改爲complex_num以防止與內建complex混淆。

最後,它們只是警告,如果需要,它們可以被忽略。

0

由於python嚴格遵循language.You應該給每個導入後兩個空格以及代碼塊。

+0

你能提供一個樣本來形象化你在說什麼? –

1

這裏是鏈接到文件: PEP8 Style Guide for Python
您應該添加的功能之間的兩個空間,如下圖所示:

import cmath 


def sqrt(): 
    try: 
     num = int(input("Enter the number : ")) 
     if num >= 0: 
      main(num) 
     else: 
      complex_num(num) 
    except: 
     print("OOPS..!!Something went wrong, try again") 
     sqrt() 
    return 


def main(num): 
    square_root = num**(1/2) 
    print("The square Root of ", num, " is ", square_root) 
    return 


def complex_num(num): 
    ans = cmath.sqrt(num) 
    print("The Square root if ", num, " is ", ans) 
    return 


sqrt() 
+0

你應該提供一個小的片段或者如何編寫的語法。 – anu

0
with warnings:- 
import math 
def my(): 
    print("hello world") 
my() 

Without warnings:- 
import math 


def my(): 
    print("hello world") 
my() 

在這裏,如果你看到import語句之後的兩行空間第二個代碼片段不會給出任何警告。 同樣,如果你正在編寫兩個方法定義,你有兩個在你的代碼塊之間給出兩行作爲空間。

0

所有答案似乎是正確的。爲了避免這樣做,你也可以使用autopep8 package(pip install autopep8)。調用autopep8 filename.py的結果是一樣的:

import cmath 


def sqrt(): 
    try: 
     num = int(input("Enter the number : ")) 
     if num >= 0: 
      main(num) 
     else: 
      complex(num) 
    except: 
     print("OOPS..!!Something went wrong, try again") 
     sqrt() 
    return 


def main(num): 
    squareRoot = num**(1/2) 
    print("The square Root of ", num, " is ", squareRoot) 
    return 


def complex(num): 
    ans = cmath.sqrt(num) 
    print("The Square root if ", num, " is ", ans) 
    return 


sqrt() 

PS:have a lookif __name__ == "__main__":

相關問題