Menu
CoddyTech

Indentation Instead of Braces

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

Use indentation to express the statement grouping you know from braces.

C# reference excerpt

Executable statements use C# top-level program style; type-only examples are declarations. System names assume the usual using System directive.

int stock = 7;
if (stock > 5) {
    Console.WriteLine("bulk");
    Console.WriteLine(stock);
}
Console.WriteLine("done");

Python

stock = 7
if stock > 5:
    print("bulk")
    print(stock)
print("done")

What changes

A colon starts the suite and indentation groups its statements. Use four spaces consistently. The final print is outside the condition because it returns to the left margin. Python does not require condition parentheses. Braces still occur in dictionaries and sets; they do not delimit an if body.

Transfer mistake

Indentation can change behavior even when the program parses. Unlike a C# braced block, an if suite does not introduce a new local-variable scope.

Your task

Read a nonnegative stock integer. Output audit. When stock exceeds 5, output bulk and then the stock value on separate lines. Output done in every case. Fix the starter indentation and missing output so the same grouping you would express with C# braces is expressed by Python suites.

challenge icon

Challenge

Beginner

Read a nonnegative stock integer. Output audit. When stock exceeds 5, output bulk and then the stock value on separate lines. Output done in every case. Fix the starter indentation and missing output so the same grouping you would express with C# braces is expressed by Python suites.

Editable implementation files: main.py. The locked driver.py is the supplied launcher or input/output harness and must remain unchanged.

Use the supplied input contract without prompts. Print only the required lines, each ending in a newline.

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 C# Developers

Practice on your own: Online Python compiler