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,

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.

2 Answers

Christian Beckett
Christian Beckett
11,118 Points

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
19,393 Points

Comment explains it well.