2015-11-01 7 views
15

मैं अजगर आईडीई के रूप में vim संपादक का उपयोग कर रहा में चेतावनी pep8, नीचे एक संख्या का वर्गमूल गणना करने के लिए एक सरल अजगर कार्यक्रम है: -उम्मीद दो रिक्त लाइनों अजगर

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 समस्याओं को ठीक होगा। आपके आयात के बाद आपको अपना कोड शुरू करने से पहले 2 नई लाइनों की आवश्यकता है। इसके अलावा, प्रत्येक def foo() के बीच आपको 2 भी होना चाहिए।

आपके मामले में आपके पास आयात के बाद 0 था, और आपके पास प्रत्येक फ़ंक्शन के बीच 1 नई लाइन थी। पीईपी 8 का हिस्सा आपको अपने कोड के अंत के बाद एक नई लाइन की आवश्यकता है। दुर्भाग्य से मुझे नहीं पता कि जब मैं अपना कोड यहां पेस्ट करता हूं तो इसे कैसे दिखाया जाए।

नामकरण पर ध्यान दें, यह पीईपी 8 का भी हिस्सा है। complex के साथ भ्रम को रोकने के लिए मैंने complex से complex_num को बदल दिया।

अंत में, वे केवल चेतावनी दे रहे हैं, यदि आवश्यक हो तो उन्हें अनदेखा किया जा सकता है।

0

चूंकि पाइथन सख्ती से भाषा का पालन कर रहा है। आपको प्रत्येक आयात के साथ-साथ कोड के ब्लॉक के बाद दो रिक्त स्थान देना चाहिए।

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

यहाँ आप आयात बयान के बाद दो पंक्तियों अंतरिक्ष देखते हैं दूसरे कोड स्निपेट के लिए जो कोई चेतावनी नहीं देगा। फिर यदि आप दो विधियों की परिभाषा लिख ​​रहे हैं तो आपके दो कोड आपके कोड ब्लॉक के बीच की जगह के रूप में दो लाइनें देते हैं।

0

सभी उत्तरों सही होने लगते हैं। हाथ से ऐसा करने से बचने के लिए, आप autopep8 package (पाइप इंस्टॉल ऑटोपैप 8) का भी उपयोग कर सकते हैं। 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() 

पुनश्च: have a lookif __name__ == "__main__": पर

संबंधित मुद्दे