Skip to content

Module 18 · Algorithms and Problem Solving — topic 3 of 18

Flowcharts

Drawing algorithms as flowcharts: standard symbols for start/end, process, input/output and decisions, representing branches and loops, and translating a flowchart into Python.

Beginner 8 min read·Lesson 263 of 306 in Python Programming

Learning objectives

  • Read and draw flowcharts with standard symbols
  • Represent decisions and loops
  • Convert a flowchart into Python code

Definition

A flowchart is a diagram of an algorithm using standard shapes connected by arrows: ovals for start and end, rectangles for processing steps, parallelograms for input and output, and diamonds for yes/no decisions.

Introduction

Before writing code, many programmers sketch the flow of a program on paper. A flowchart makes branches and loops visible at a glance.

Why it matters

Flowcharts help you think through logic, explain it to others and spot missing cases — such as what happens when the third PIN attempt fails.

Detailed explanation

An oval marks Start and End. Every flowchart has exactly one start.

A rectangle is a process step: a calculation or assignment such as total = total + price.

A parallelogram is input or output: read a number, display a result.

A diamond is a decision with a yes/no question; each answer leads along a labelled arrow. Decisions become if/elif/else in code.

An arrow that goes back to an earlier step forms a loop, which becomes a while or for loop in code.

Keep flowcharts top-to-bottom, label every decision branch, and avoid crossing lines where possible.

Each path through the flowchart is a scenario to test, which makes flowcharts useful for designing test cases.

For large programs, flowcharts become unwieldy; pseudocode and code structure then take over, but small flowcharts remain useful for tricky logic.

Syntax

Text
( Start )
|
/ Read marks /
|
< marks >= 50? > --no--> [ result = "Fail" ]
| yes |
[ result = "Pass" ] |
|<--------------------------+
/ Print result /
|
( End )
  • Ovals: start/end.
  • Diamonds: decisions.
  • Back-arrows: loops.

Code examples

A grading flowchart in text form

Text
( Start )
|
/ Read marks /
|
< marks >= 90? > --yes--> [ grade = A ]
| no
< marks >= 75? > --yes--> [ grade = B ]
| no
< marks >= 50? > --yes--> [ grade = C ]
| no
[ grade = F ]
|
/ Print grade /
|
( End )

The same flowchart in Python

Python
def grade(marks):
if marks >= 90:
return "A"
elif marks >= 75:
return "B"
elif marks >= 50:
return "C"
return "F"
print([grade(m) for m in [95, 80, 60, 30]])
Output
['A', 'B', 'C', 'F']

More examples

A loop with a limit: PIN attempts

Python
def check_pin(attempts, correct="4321", limit=3):
for i, pin in enumerate(attempts[:limit], 1):
if pin == correct:
return f"access granted on attempt {i}"
print(f"attempt {i}: wrong PIN")
return "card blocked"
print(check_pin(["1111", "4321"]))
print(check_pin(["0000", "1234", "9999", "4321"]))
Output
attempt 1: wrong PIN
access granted on attempt 2
attempt 1: wrong PIN
attempt 2: wrong PIN
attempt 3: wrong PIN
card blocked

Step-by-step explanation

  • In the flowchart, a decision 'PIN correct?' has a yes arrow to 'grant access' and a no arrow back to 'read PIN'.
  • A second decision 'attempts < 3?' stops the loop; in code that is the slice attempts[:limit].
  • The fourth PIN in the second call is never checked because the card is blocked first.

Expected output

Running “A loop with a limit: PIN attempts” prints exactly:

TextOutput
attempt 1: wrong PIN
access granted on attempt 2
attempt 1: wrong PIN
attempt 2: wrong PIN
attempt 3: wrong PIN
card blocked

Practical applications

  • Documenting business processes such as loan approval.
  • Planning game logic and menu flows.

Best practices

  • Use standard symbols consistently.
  • Label every decision branch.
  • Test each path through the chart.

Common mistakes

Unlabelled decision branches

Avoid

< valid? > --> ... --> ...

Prefer

< valid? > --yes--> ... --no--> ...

Without labels, readers can't tell which path is which.

Important notes

  • Tools such as draw.io and Mermaid can draw flowcharts from text.

Key takeaways

  • Ovals start and end, rectangles process, parallelograms do I/O, diamonds decide.
  • Back-arrows show loops.
  • Each path is a test case.

Try it yourself

  1. 1
    Draw a flowchart for deciding whether a year is a leap year, then implement it.
  2. 2
    Draw a flowchart for a number-guessing game with five attempts.

Finished reading? Mark it complete to update your dashboard.

Suggested practice

16 questions cover the “Algorithms and Problem Solving” module. Answers and explanations are revealed after you submit.

  • AdvancedWhat is printed?
  • AdvancedWhat is the average time complexity of x in my_set?
  • AdvancedA loop over a list checks item in other_list for each item. With both lists of length n, the complexity is…
Practise this module