Chapter 3: If statements

Python indentation

face Josiah Wang

Python requires you to indent any statements inside your block.

This makes sense. Python needs to know what belongs inside your if statement, and what belongs outside.

You can indent with either spaces or tabs, but you may NOT mix spaces and tabs.

Python officially recommends using four spaces for indenting your code blocks.

if user_guess == 42:
    print("Correct.")
    print("Very clever!")
else:
    print("Incorrect.")
    print("Try again.")

For our course, you may indent your code with either four spaces or a tab.

But you must NOT mix spaces and tabs inside a single file. I will consider this a mortal sin and will cast you into Snake Hell (TM) if I ever see this in your coursework submissions. 🔥🐍🔥🐍🔥🐍🔥

Although I might be compassionate and just deduct marks from your grade instead 😈😈

Python will generally not let you run such codes anyway. It will usually result in a Syntax Error.

Some text editors can automatically convert your tabs to spaces as you type. For example, in Notepad++ on Windows, you can go to Settings > Preferences to enable this.

Automatically converting tabs to spaces in Notepad++