2015-11-01 13 views
15

Sto usando l'editor vim come IDE per Python, di seguito è un semplice programma Python per calcolare la radice quadrata di un numero: -attesi due righe vuote PEP8 avviso in pitone

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() 

e gli avvertimenti sono: -

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] 

Potete dire per favore perché questi avvertimenti stanno arrivando?

enter image description here

+1

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

risposta

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() 

Il precedente risolverà i vostri problemi PEP8. Dopo l'importazione è necessario disporre di 2 nuove righe prima di iniziare il codice. Inoltre, tra ogni def foo() è necessario avere anche 2.

Nel tuo caso hai avuto 0 dopo l'importazione, e hai avuto 1 newline tra ogni funzione. Parte di PEP8 è necessario avere una nuova riga dopo la fine del codice. Purtroppo non so come mostrarlo quando incollo il tuo codice qui.

Prestare attenzione alla denominazione, è parte di PEP8 pure. Ho cambiatoin complex_num per evitare confusione con l'integrato complex.

Alla fine, stanno solo avvisando, possono essere ignorati se necessario.

0

Come python segue strettamente la lingua. È necessario fornire due spazi dopo ogni importazione e blocco di codice.

+0

Potete fornire un esempio per visualizzare ciò che stai dicendo? –

1

ecco il link alla documentazione: PEP8 Style Guide for Python
Si dovrebbe aggiungere due spazi tra le funzioni, come illustrato di seguito:

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

Dovresti fornire un piccolo snippet o sintassi su come scriverlo. – anu

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

Without warnings:- 
import math 


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

Qui se si vede lo spazio di due righe dopo istruzione import per il secondo snippet di codice che non fornirà alcun avviso. Anche se stai scrivendo la definizione di due metodi hai due righe due come spazio tra il tuo blocco di codice.

0

Tutte le risposte sembrano corrette. Per evitare di farlo a mano, è anche possibile utilizzare il autopep8 package (autopep8 di installazione del pip). Il risultato della chiamata autopep8 filename.py è lo stesso:

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 look a if __name__ == "__main__":