Menu
CoddyTech

Blocks Use Indentation

Lesson 5 of 48 in Coddy's Python for JavaScript Developers course.

Make the intended block visible through indentation rather than braces.

JavaScript

const seats = 4;
if (seats > 0) {
  console.log("open");
  console.log(seats);
}
console.log("checked");

Python

seats = 4
if seats > 0:
    print("open")
    print(seats)
print("checked")

What changes

The two programs print the same three lines. Python ends an if header with a colon and indents the statements controlled by it. The comparison > asks whether the left number is greater than the right. Statements at the same indentation belong to the same block. Returning to the earlier indentation level ends that block; there is no closing brace. Use four spaces per level consistently. More detailed choices and alternative branches arrive in the next chapter. A syntax error report names the file and line and usually marks a nearby location with a caret. Read the final error description, inspect that location, and check the preceding header too.

Common mistake

Moving a line back to the margin makes it unconditional, even if it still looks visually close to the condition. An absent colon can cause SyntaxError; a missing block indent can cause IndentationError. Mixing tabs with spaces can also fail. Fix the structure before reasoning about output.

Your task

Keep the audit and final message outside the condition, and make the two bulk messages share one indented block. Test both sides of the threshold.

challenge icon

Challenge

Beginner

Read a nonnegative integer stock level. Print audit first. If stock is greater than 5, print bulk and then the stock number. Always print done last. Repair the block layout and add the missing output; no other lines should appear.

Print only the requested output, with a newline after each line. The supplied input lines read the test data; keep them and add your work below.

Your code goes in main.py. The locked driver.py starts your program and cannot be edited.

Try it yourself

# Starts the program in main.py. Edit main.py to solve the challenge.
from runpy import run_module

run_module("main", run_name="__main__")
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Python for JavaScript Developers

Practice on your own: Online Python compiler