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 trial

Python Write Better Python Cleaner Code Function and Class Whitespace

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

Write Better Python, white spaces

This code looks correct to me, but I get an error.

starter.py
def first_function(arg1):
    return 'arg1 is {}'.format(arg1)


def second_function(arg1):
    return 'arg1 is {}'.format(arg1)


class MyClass:
    args = [1, 2, 3]

    def class_func(self):
        return self.args
Elizabeth McInerney
Elizabeth McInerney
3,175 Points

The instructions were: "Add the correct amount of whitespace between the functions, between the functions and the class, and before the class method."

The error message I got with the above code was: "xBummer! Your file has 6 error(s) on line(s) 3,4,7,8,11,13: no newline at end of file; blank line contains whitespace.

7 Answers

Colin Bell
Colin Bell
29,679 Points

Make sure there aren't any spaces on the blank lines. When you push enter, it automatically adds spaces so delete those.

Also, add a blank line at the end file. No spaces on it either.

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

Thanks, this worked, but I don't understand it. I don't recall the instructor covering spaces on blank lines appearing when you hit enter, or adding a blank line to the end of the file.

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

There is a style checker called pylint. One of the checks ["C303"] looks if there is any whitespace before an end-of-line character. While trailing white space doesn't violate python syntax, it can be viewed as superfluous.

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

How is a person doing this challenge supposed to know that, given what we have learned so far and given that we cannot see whitespace on the screen? I think Treehouse should take the whitespace error flag out of this challenge.

Colin Bell
Colin Bell
29,679 Points

The error it gives let's you know what to look for and where to look for it.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Python, as an interpreter, is not so picky. This challenge introduces the "pickiness" of coding style standards such as PEP-8. PEP-8 is simply a guideline for helping coders converge on a single style to use across a project. It helps on-boarding new programmers to a project by presenting code in a consistent style and makes code more portable for including in other projects when the styles match.

This "fluff" checking is also called "lint" There are static style "lint" checkers such as pylint and flake8 that will look for unused variables, trailing spaces and lines between functions, etc. Some programming projects stipulate using lint checkers before submitting code. As a coder it is sometimes easier to pound out code then run the lint check afterwards, and there are some IDE and text editors that have on-the-fly lint checking available.

Casey Huckel
PLUS
Casey Huckel
Courses Plus Student 4,257 Points

Thanks Chris. Another question: I've been studying Python for a couple months now and I still struggle with it. How long does it take to grasp Python and actually become "fluent," to the point that you can be employed as a Pythonista or computer programmer?

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

I do no know enough about interpreting errors to have interpreted this one. Plus, it sounds like this was only an error on the course console, it would not be an error if I had written this code outside. So it is not helping the student to have it in there, given that it covered a topic that was not in the video.

I get a syntax error stating: SyntaxError: def class_func(self):

I have the same code as Elizabeth and removed all the white spaces.