Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Technical Interview Prep: Python Basics Basic Python And I Will Try To Fix You

Travers Geoffray
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Travers Geoffray
Python Development Techdegree Graduate 26,967 Points

Getting error that there should a be a blank line before a method. Not sure what method it's referring to...

pep.py
import datetime


def my_func():
    return 'It ran!'


sizes = ['small', 'medium', 'large']


class Tree:


    def __init__(self, size, characteristics):
        self.size = size
        self.charac = characteristics
        self.roots = True
        self.leaves = 0


    def grow_leaves(self):
        self.leaves += 1

Tree(sizes[0], {'name': 'Tulip Tree'})
Shane Peter
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Shane Peter
Python Development Techdegree Graduate 8,556 Points

With PEP8 you only need one blank line After each method within a class, so the blank lines between "class Tree:" and your first def are not needed as well as only one blank line between self.leaves = 0 and your second def. Lastly each function/class needs to be separated by two blank lines which means you'll need to add another line above "Tree(sizes...." A good way to think of blank lines are like breathing/brake points when speaking/writing, it gives your mind and eyes a rest and helps differentiate between sections of code. Hope this helps:D

4 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,982 Points

Hey Travers Geoffray, the preview window is showing a different error:

AssertionError: Regex didn't match: 'self.leaves\\s\\+\\=\\s1[\\r\\n]{3}Tree' not found...

There should be 2 blanks lines after a function or class

So add another blank line before the instantiation of Tree and it should pass.

BTW, though the checker is looking for "at least x lines", in cases of the methods, one blank line is sufficient. A handy local function to have is pycodestyle that checks all the syntax.

$ pip install pycodestyle
$ pycodestyle your_code.py
$ pycodestyle your_code.py 
your_code.py:14:5: E303 too many blank lines (2)
your_code.py:21:5: E303 too many blank lines (2)
your_code.py:24:1: E305 expected 2 blank lines after class or function definition, found 1

Post back if you have more questions. Good luck!!

Travers Geoffray
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Travers Geoffray
Python Development Techdegree Graduate 26,967 Points

Chris - thank you so much for your help! Unfortunately, I'm still getting the same error after adding another blank line before the instantiation of Tree....

Chris Freeman
Chris Freeman
Treehouse Moderator 67,982 Points

Did you add the blank line marked my >>> below? Be sure the editor didn’t add any spaces on the line.

        def grow_leaves(self):
            self.leaves += 1

>>> 
    Tree(sizes[0], {'name': 'Tulip Tree'})

If your still getting the error please repost your latest code.

Travers Geoffray
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Travers Geoffray
Python Development Techdegree Graduate 26,967 Points

I did, I think I'm doing it correctly...here's the code:

import datetime


def my_func():
    return 'It ran!'


sizes = ['small', 'medium', 'large']


class Tree:
    def __init__(self, size, characteristics):
        self.size = size
        self.charac = characteristics
        self.roots = True
        self.leaves = 0


    def grow_leaves(self):
        self.leaves += 1


Tree(sizes[0], {'name': 'Tulip Tree'})
Chris Freeman
Chris Freeman
Treehouse Moderator 67,982 Points

Lines 17, 18, 21, and 22 have spaces. Spaces are considered characters and are not “blank”.

Travers Geoffray
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Travers Geoffray
Python Development Techdegree Graduate 26,967 Points

Ah okay...how do I make the line blank instead of a space? I'm just using return key to separate the lines of code. Is that the wrong way to do it?

Chris Freeman
Chris Freeman
Treehouse Moderator 67,982 Points

After adding a newline, put the cursor at the end of the line and delete any spaces.