Egg box
2 rows, and 6 eggs in each row. That is 12 eggs.
05LOOPS · LOOPS INSIDE LOOPS
Put one loop inside another. The inside loop runs all its turns on every turn of the outside loop.
IN REAL LIFE
A big repeat, with a small repeat inside each turn.
2 rows, and 6 eggs in each row. That is 12 eggs.
Every day of the week, you eat breakfast, lunch and dinner.
After each verse, sing the chorus two times.
LOOK FIRST
It starts a turn, then waits for the inside loop to finish.
It runs all its turns, every time the outside loop takes a turn.
2 outside turns × 3 inside turns = 6 times in all.
LOOK FIRST
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
Guess first. Then run real Python and check.
for row in range(3):
line = ""
for star in range(4):
line = line + "*"
print(line)What will Python draw?
PyBot's hint
Ready. Press Run Python.
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
Try each tiny question. A wrong answer becomes something to review.
A box has 2 rows. Each row has 6 eggs.
How many eggs are there?
for a in range(3):
for b in range(2):
print("hi")How many times does hi show?
for row in rows:
for seat in seats:Which loop finishes all its turns first?
for letter in ["A", "B"]:
for n in [1, 2]:
print(letter, n)Python shows A 1 first. What shows second?
for row in range(2):
for seat in range(3):
print("seat")
print("row done")How many times does row done show?
total = 0
for day in range(7):
for meal in range(3):
total = total + 1
print(total)What does Python show?
for word in ["hi", "yo"]:
for letter in word:
print(letter)How many lines does Python show?
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.
*** ***OUTPUT
Want a fresh start? Clear only this page.
The inside loop runs all its turns on every outside turn. Next, you will learn to ask sharper questions with True and False.