expected two blank lines pep8 warning in python

前端 未结 5 2022
不知归路
不知归路 2020-12-25 13:44

I\'m using vim editor as python IDE. Below is a simple python program to calculate square root of a number:

import cmath
def sqrt():
    try:
        num = i         


        
相关标签:
5条回答
  • 2020-12-25 14:19
    with warnings:-  
    import math  
    def my():  
        print("hello world")  
    my()
    
    Without warnings:-  
    import math 
    
    
    def my():  
        print("hello world")  
    my()
    

    Here if you see the two lines space after import statement for second code snippet which will not give any warnings. Again if you are writing two methods definition you have two give two lines as space between your code block.

    0 讨论(0)
  • 2020-12-25 14:21
    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()
    

    The previous will fix your PEP8 problems. After your import you need to have 2 new lines before starting your code. Also, between each def foo() you need to have 2 as well.

    In your case you had 0 after import, and you had 1 newline between each function. Part of PEP8 you need to have a newline after the end of your code. Unfortunately I don't know how to show it when I paste your code in here.

    Pay attention to the naming, it's part of PEP8 as well. I changed complex to complex_num to prevent confusion with builtin complex.

    In the end, they're only warning, they can be ignored if needed.

    0 讨论(0)
  • 2020-12-25 14:26

    Here is the link to the documentation: PEP8 Style Guide for Python
    You should add two spaces between the functions, as shown below:

    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 讨论(0)
  • 2020-12-25 14:28

    All answers seem to be correct. To avoid doing this by hand, you can also use the autopep8 package (pip install autopep8). The result of calling autopep8 filename.py is the same:

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

    0 讨论(0)
  • 2020-12-25 14:34

    You need to give two blank lines between meaningful code blocks.

    These include (for example):

    • The import block
    • Each function
    0 讨论(0)
提交回复
热议问题