←Previous page

05LOOPS · LOOPS INSIDE LOOPS

A loop inside a loop.

Put one loop inside another. The inside loop runs all its turns on every turn of the outside loop.

IN REAL LIFE

Loops inside loops in real life.

A big repeat, with a small repeat inside each turn.

Egg box

2 rows, and 6 eggs in each row. That is 12 eggs.

Days and meals

Every day of the week, you eat breakfast, lunch and dinner.

A song

After each verse, sing the chorus two times.

LOOK FIRST

Outside and inside.

The outside loop

It starts a turn, then waits for the inside loop to finish.

The inside loop

It runs all its turns, every time the outside loop takes a turn.

Multiply the turns

2 outside turns × 3 inside turns = 6 times in all.

LOOK FIRST

Walk through two days of meals.

Watch the spaces at the start of each line.

for day in ["Mon", "Tue"]:The outside loop: 2 days.
for meal in ["breakfast", "lunch", "dinner"]:The inside loop: 3 meals. It is moved right, so it is inside the day loop.
print(day, meal)Moved right two times: it runs 3 times for each day. Mon breakfast, Mon lunch...
print("bedtime!")Inside the day loop only: once at the end of each day.

RUN IT

Help PyBot draw a box of stars.

Guess first. Then run real Python and check.

1

Guess the result

for row in range(3): line = "" for star in range(4): line = line + "*" print(line)

What will Python draw?

stars.pyREAL PYTHON
OUTPUT
Ready. Press Run Python.
Python runs here thanks to Pyodide ♥

Pyodide 314.0.3 runs CPython in your browser with WebAssembly.

Open-source under the Mozilla Public License 2.0. License

Now change one thing: Change 4 to 6 and run again. Then change 3 to 5.

YOUR TURN

Loops inside loops.

Try each tiny question. A wrong answer becomes something to review.

0Completed
9Not tried
0Review
01

Egg box

A box has 2 rows. Each row has 6 eggs.

How many eggs are there?

02

Count the hi's

for a in range(3): for b in range(2): print("hi")

How many times does hi show?

03

Who finishes first?

for row in rows: for seat in seats:

Which loop finishes all its turns first?

04

What comes second?

for letter in ["A", "B"]: for n in [1, 2]: print(letter, n)

Python shows A 1 first. What shows second?

05

Once per row

for row in range(2): for seat in range(3): print("seat") print("row done")

How many times does row done show?

06

A week of meals

total = 0 for day in range(7): for meal in range(3): total = total + 1 print(total)

What does Python show?

07

Words, then letters

for word in ["hi", "yo"]: for letter in word: print(letter)

How many lines does Python show?

08

Fix PyBot's code

PyBot wants two rows of 3 stars, but the second row is too long. The empty line box must start again on every row. Move it inside the row loop.

Goal output
***
***
OUTPUT

Your work is saved here.

Want a fresh start? Clear only this page.

!
BIG IDEA

Inside a loop, you can put another loop.

The inside loop runs all its turns on every outside turn. Next, you will learn to ask sharper questions with True and False.