Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

expected two blank lines pep8 warning in python

Tags:

python

vim

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

And the warnings are :

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]

Can you please tell why these warnings are coming?

enter image description here

like image 596
Amit Upadhyay Avatar asked Nov 01 '15 20:11

Amit Upadhyay


People also ask

Are Blank lines forbidden in Python?

Extra blank lines may be used (sparingly) to separate groups of related functions. Blank lines may be omitted between a bunch of related one-liners (e.g. a set of dummy implementations). Use blank lines in functions, sparingly, to indicate logical sections. Python accepts the control-L (i.e.

How many blank lines between functions?

Functions and classes should have two blank lines after them, separating them from other functions and classes.

How many blank lines should be before and after method definitions inside a class method definitions inside a class are just another term for functions in a class?

One blank line should be both before and after method definitions. You should use blank lines conservatively within your code to separate groups of functions.

What are blank lines in code?

Blank lines improve readability by setting off sections of code that are logically related. Two blank lines should always be used in the following circumstances: Between sections of a source file. Between class and interface definitions.


3 Answers

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.

like image 186
Leb Avatar answered Oct 06 '22 09:10

Leb


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.

like image 43
Balaji Wanole Avatar answered Oct 06 '22 10:10

Balaji Wanole


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()
like image 2
Lakshmikant Deshpande Avatar answered Oct 06 '22 09:10

Lakshmikant Deshpande