50

Consider the following code:

def add_function(a, b):
    c = str(a) + b
    print "c is %s" % c

def add_int_function(c, d):
    e = c + d
    print "the vaule of e is %d" % e

if __name__ =="__main__":
    add_function(59906, 'kugrt5')
    add_int_function(1, 2)

It always shows me: "expected 2 blank lines ,found 1" in aadd_int_function, but not in the add_function.

When I add two spaces in front of the def add_int_function(c, d): there is a error shows unindent does not match any outer indentation level in the end of add_function:

enter image description here

enter image description here

TT--
  • 2,956
  • 1
  • 27
  • 46
march_seven
  • 691
  • 1
  • 7
  • 13
  • If you find and answer useful, accept that answer by clicking on its check-mark (like [here](http://i.stack.imgur.com/QpogP.png))so that other people will know the answer worked for you in the first look – Kennet Celeste Oct 27 '16 at 12:24
  • PyCharm will fix it for you if you click on the code or press **Alt-Enter** and then click the yellow lightbulb and select **Reformat file** – TT-- Feb 07 '18 at 03:16

4 Answers4

104

Just add another line between your function definitions :

1 line :

enter image description here

2 lines:

enter image description here

Kennet Celeste
  • 4,593
  • 3
  • 25
  • 34
9

This is a pretty common question within the python community. After the release of PEP 8, new formatting styles were accepted into python. One of them states that after the definition of a class or function there must be two lines separating them. As such:

    def yadayada:
     print("two lines between the functions")


    def secondyadayada:
     print("this is the proper formatting")

So, you should never do it like:

    def yadayada:
     print("two lines between the functions")

    def secondyadayada:
     print("this is the proper formatting")

Or else PyCharm will throw that error at you.

shreyshrey
  • 515
  • 6
  • 20
2

For people who wonders why it requires two blank lines

if you were to write in other languages it would be:

fun doSth() {
    print()
}

fun doSth1() {
    print()
}

but if you were to delete all the curly braces from the code you will see:

two blank lines between methods

fun doSth()
    print()
#
#
fun doSth1()
    print()
#
adwardwo1f
  • 817
  • 6
  • 18
1

Further clarification on @kennet-celeste & @shreyshrey 's answers,

Each function or class defined requires 2 spaces above and 2 spaces below. Unless the function is the last item in the script, in which the expected format is one blank line as an End of File marker. So:

# some code followed by 2 blank spaces


def function1():


def function2():


def function3():
M_Merciless
  • 379
  • 6
  • 12