Nesting, Combining, and Loops Together

Two ways to express a compound decision:

Combine with and / or, one condition, cleaner

Nest an if inside another, when the inner check only makes sense after the outer one passes

Prefer combining. Reach for nesting when the steps are genuinely sequential.

EXAMPLE
loading editor…
YOUR TURN
Your turn → Set has_ticket to False and run it. Then rewrite it as a NESTED if, one if inside another, to feel the difference in readability.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

Now everything joins: a conditional inside a loop, running a decision on every record. This is what nearly all real data processing looks like.

EXAMPLE
loading editor…
YOUR TURN
Your turn → Collections + loops + conditionals + accumulation, all at once. Add two more students, one above 85 and one below, then add a counter that prints how many students are still building.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

◈ Graded Challenge

The variable score holds 85. Create a variable named grade holding "A" for 90 and above, "B" for 80 to 89, "C" for 70 to 79, and "Keep going" below that. Order your conditions carefully.

Name it exactly: grade

2 visible + 1 hidden
loading editor…

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Practice Test →
← Back