Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTravers Geoffray
Python Development Techdegree Graduate 26,967 PointsGetting error that there should a be a blank line before a method. Not sure what method it's referring to...
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'})
4 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsHey 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
Python Development Techdegree Graduate 26,967 PointsChris - 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
Treehouse Moderator 68,457 PointsDid 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
Python Development Techdegree Graduate 26,967 PointsI 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
Treehouse Moderator 68,457 PointsLines 17, 18, 21, and 22 have spaces. Spaces are considered characters and are not βblankβ.
Travers Geoffray
Python Development Techdegree Graduate 26,967 PointsAh 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
Treehouse Moderator 68,457 PointsAfter adding a newline, put the cursor at the end of the line and delete any spaces.
Travers Geoffray
Python Development Techdegree Graduate 26,967 PointsThat did it, thank you Chris! I appreciate your patience.
Shane Peter
Python Development Techdegree Graduate 8,556 PointsShane Peter
Python Development Techdegree Graduate 8,556 PointsWith 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