Module 2 — Decisions and Repeats
Teach your program to choose a path and to do the same job many times.
3 lessons · about 25 min · difficulty 2 of 5
1. if / elif / else
One question, one indented block of work for each possible answer.
Python uses indentation, not braces. Four spaces is the standard.
`elif` checks another condition only when the ones above it were False.
Think of it like: A dispatcher at a crossroads waving each truck down exactly one road.
At NoblePort: Evidence classification is a ladder of ifs: installed, connected, configured, staged, tested, verified live. The status board colour for every NobleHub module comes from one of these chains.
Try it yourself: Turn the score example into an evidence check: print 'Verified live' at 90+, 'Tested' at 70+, otherwise 'Not trusted yet'.
Common mistake: Using `if` three times in a row when you meant `elif`. Independent ifs can all fire and print three different answers.
Why it matters: Every routing rule and approval threshold is an if statement.
2. Loops do the boring part
A for loop walks through a list one item at a time.
`for item in items:` runs the indented block once for each item.
Use `range(n)` when you want to repeat something a fixed number of times.
Think of it like: Walking the punch list room by room — same clipboard, one room at a time.
At NoblePort: The reminder job that emails NoblePort learners about due exercises is a loop: for each learner, count what is due, then queue one message. Three learners or three thousand, the code is identical.
Try it yourself: Loop over five jobs and print a scheduling line only for the ones whose name contains 'deck'.
Common mistake: Changing a list while looping over it. Build a new list instead of editing the one you are walking.
Why it matters: This is how one script handles 3 records or 30,000 without changing.
3. Lists and dictionaries
Lists keep things in order; dictionaries look things up by name.
A list uses square brackets and numbered positions starting at 0.
A dictionary uses curly braces with key/value pairs, so you fetch by label instead of position.
Think of it like: A list is the stack of job jackets in order; a dictionary is the label tabs you grab by name.
At NoblePort: One NobleHub job jacket is a dictionary: `{"job": "Harbor Deck", "permit": "approved", "crew_hours": 62}`, and the week's schedule is a list of those jackets.
Try it yourself: Build a job jacket dictionary with four fields, put three of them in a list, and print each job's permit status.
Common mistake: Reaching for `crew["lead"]` on a key that does not exist. Use `crew.get("lead", "unassigned")` when a field might be missing.
Why it matters: Almost every AI API answer arrives as a dictionary of lists — reading them is the skill.