GCSE · Computer Science · OCR · Spec J277

Maintainability: indentation

Two programs. Same words, same order, same score. One of them tells someone who failed "Well done!". The only difference? Four spaces.

Computer Science · Program layout

Same words, different program

Two Python programs. Same words, same order. Only line 11 has lost its indent. Step through both and watch which lines run.

score is 35, so the if test is False. Before you press Next: what do you think each version prints?

1 # Version A
2 score = 35
3 if score >= 50:
4 print("You passed!")
5 print("Well done!")
6 print("Results sent")
7 # Version B
8 score = 35
9 if score >= 50:
10 print("You passed!")
11 print("Well done!")
12 print("Results sent")

Variables

score=35

Output

 

Step 1: Version A starts. score is set to 35.

1 / 7

Press Next and watch which lines light up. The output box keeps a running record, marked A or B.

Step 1 of 7: Version A starts. score is set to 35..

Watch out: Un-indenting a line doesn't always stop Python running the program. Version B runs perfectly happily. It just does the wrong thing.

Python vs languages that don't need indentation

PythonvsA language where indentation isn't required

Read across each row. The top row is the one people mix up most.

Focus

What indentation is

Python

Part of the syntax. It decides how the program is executed.

A language where indentation isn't required

Not required. The language doesn't need it to run the code.

The insight

So 'indentation is just for neatness' is only half true, and it's the wrong half for Python.

Leave the selection block un-indented

Python

The program won't run at all.

A language where indentation isn't required

The program still runs.

What leaving it out costs

Python

A program that doesn't run, or one that does something you didn't mean.

A language where indentation isn't required

Code that is harder to read, so harder to maintain.

Is indenting selection blocks good layout?

Python

Yes, and here good layout is also compulsory.

A language where indentation isn't required

Yes. It's optional for the computer, not for the humans reading it.

Computer Science · Program layout

Find the line that's in the wrong place

This program should charge 5 for anyone under 16 and 8 for everyone else. A 15-year-old is charged 8. One line is indented wrongly. Which one?

A classmate's ticket program — which line goes wrong?

Layout and maintainability

?

Reason it through

Why does a consistent, logical layout make a program easier to maintain?

Link 1 of 3

First link · your turn

The code is laid out consistently and logically, with every selection block indented. What does that do for another programmer reading it?

2
Locked — reveal the link above first
3
Locked — reveal the link above first

Computer Science · Program layout

"It runs, so who cares?"

Sam writes code in a language where indentation isn't required. Every line sits hard against the left margin, selection blocks included. The program runs perfectly. "Layout doesn't matter," says Sam. "Indentation is a Python thing."

Which is closest to what you think about Sam's code?
How sure are you?

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

A few spaces at the start of a line can change what a program does, and decide whether anyone can fix it later.

What you need to know

  • Maintainability is how easily maintenance jobs, such as correcting errors, can be done on code.
  • Have a goTwo programs do the same job. One is a nightmare to correct when something goes wrong. Is that a problem with its speed or its maintainability?

    Its maintainability.

    Maintainability is about how easily maintenance like correcting errors can be done. How fast the program runs is a different question entirely.

  • A consistent, logical layout makes code much easier for other programmers to understand.
  • Indentation, starting lines further in from the left margin, is a vital part of that layout.
  • In Python, indentation is part of the syntax: it decides how the program is executed.
  • Python knows which lines belong to an if block because they are indented underneath it.
  • Have a gox is 1. Under the line if x > 3: sits print('big'), indented. Below that is print('done'), not indented. What gets printed?

    done

    Only the indented line belongs to the if, and the test is False, so it's skipped. print('done') isn't in the block, so it runs whatever the test says.

  • Indenting selection blocks is good layout. In Python, a program whose if block has no indented lines won't run.
  • Un-indent one line from inside a block and the program may still run, but do something different.
  • Have a goYour friend un-indents a line inside an if block. The program still runs. "Told you indentation doesn't matter!" What have they missed?

    That line has left the block, so it now runs even when the condition is False.

    No error doesn't mean no change. The program runs, but it behaves differently, because the line no longer belongs to the if.

  • In some other languages indentation isn't required, but code without it is harder to read.
  • Skip good layout and code can become difficult to maintain or fix, and other programmers may not understand it.
  • Even the person who wrote the code may struggle to follow it after time away.

The big picture

Maintainability is how easily maintenance, such as correcting errors, can be done on code. A consistent, logical layout makes code much easier for other programmers to understand, and indentation is a vital part of it. In Python, indentation is part of the syntax: it decides which lines belong to a selection block, and a program whose if block has no indented lines won't run. In some other languages it isn't required, but code without it is harder to read, harder to maintain, and harder even for its author to follow later.

Key points

1Maintainability: the ease with which maintenance activities, such as correcting errors, can be performed on code.
2Consistent, logical layout makes code much easier for other programmers to understand. Indentation is a vital part of it.
3Python: indentation is part of the syntax and decides how the program is executed, including which lines belong to a selection block.
4Python won't run a program whose selection block has no indented lines. A line indented wrongly can make it run differently.
5Languages where indentation isn't required: the code runs without it but is harder to read.
6Poor layout makes code difficult to maintain or fix, hard for other programmers to understand, and hard for its author after time away.

Worked example

Problem

A Python program has five lines. Line 1: mark = 72. Line 2: if mark >= 70: Line 3, indented by 2 spaces: print('Grade A'). Line 4, not indented: else: Line 5, indented by 8 spaces: print('Grade B'). Does it run? What does it print? And how would you improve its layout?

⚠ Watch out

Thinking indentation is just for neatness. It feels true if your code has run fine without it. But in Python, indentation decides which lines belong to a block, so moving one line can change what the program does. Elsewhere, leaving it out still makes code harder to read and maintain.

🧠

Memory hook

In Python, the spaces decide what runs. In every language, the spaces decide who can follow it.

✓

Check yourself

Explain to a friend how two Python programs with exactly the same words in the same order can print different things. Then explain why indentation still matters in a language that doesn't need it.

Flashcards

(9)
What is maintainability?
The ease with which maintenance activities, such as correcting errors, can be performed on code.
Why should a program have a consistent, logical layout?
It makes the code much easier for other programmers to understand.
Which part of program layout is described as vital?
Indentation.
In Python, what is indentation part of?
The syntax. It decides how the program is executed.
How does Python tell which lines are inside an if block?
They are the lines indented underneath it.
Python: what happens if a selection block has no indented lines?
The program won't run.
In a language that doesn't require indentation, what happens if you leave it out?
The code still runs, but it is harder to read.
A line inside a Python if block is moved back to the left margin. What changes?
It is no longer part of the block, so it runs whether or not the condition is True.
Three consequences of not following good layout?
Code is difficult to maintain or fix; other programmers may not understand it; the original programmer may struggle after time away.

Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.

Learning with Lightbulb is opening soon

You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.

Keep me posted

More OCR GCSE Computer Science topics

See the full OCR Computer Science curriculum →

How this lesson was checked. This OCR GCSE Computer Science (specification J277)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 3 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.