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 PEP 8

Whitespace?

I was running the file in the workspace, cleaning up everything as Kenneth did but I keep getting errors telling me to get rid of whitespace. What does this mean and how do I prevent it from happening,

3 Answers

Hi, would you mind showing your code? Ensure that you copy the entire page (CTRL + A to be safe) so I can see if there is any erroneous whitespace.

Essentially, python is one of the few programming languages that makes use of whitespace to denote codeblocks and terminate certain expressions. For example take the following in Javascript:

if (1 > 2) {
    console.log("Something is wrong");
}

In Javascript, This could also be written as:

if (1 > 2) {console.log("Something is wrong");}

Because Javascript relies on the parenthesis () and braces {} to denote the entire code block.

In python, you rely on whitespace (horizontal and vertical whitespace - so spaces, tabs, carriage returns, newlines, etc...) to indicate codeblocks. The same expression in python would be:

if 1 > 2:
    print("Something is wrong")

In python, the boolean expression is not wrapped in parenthesis and the code block is indented rather than contained in braces. The whitespace is important for the interpreter to correctly parse your code.

Common mistakes with whitespace in python can include mixing tabs and spaces (use either one but don't mix and match unless you know what you are doing and/or using an IDE that can handle the correct fixes for you) or not including enough newlines after function declarations.

If you post your code, I'd be happy to help.

when you refer to whitespace? I'm assuming you don't know what it means. because it was never explained. I understand that might be confusing but whitespace is just the name they use for spaces between code that aren't exactly needed. therefore called white space because the background of the workspace is white. and since most work is done on the workspace therefore. white background + workspace document = whitespace

Josh Keenan
Josh Keenan
20,315 Points

Comment explains it well.