Add lesson 4: Loops that do the work

Mira Kovac 1 year ago
parent b13d696277 commit 8d397225cf34958752262464f6c17d833281d466
3 changed files with 39 additions and 0 deletions
+19-0lessons/04-loops-that-do-the-work/README.md ADDED
@@ -0,0 +1,19 @@
1+# Lesson 4: Loops that do the work
2+
3+In this lesson you'll work through loops that do the work step by step, with short exercises after each idea.
4+
5+Full lesson: [https://glibcode.net/courses/python-basics/lessons/4](https://glibcode.net/courses/python-basics/lessons/4)
6+
7+## Files
8+
9+- `loops.py`: for and while loops over a shopping list
10+
11+## Run it
12+
13+```sh
14+python3 loops.py
15+```
16+
17+## Exercises
18+
19+See [exercises.md](exercises.md). Ask in the forum once you've tried.
+5-0lessons/04-loops-that-do-the-work/exercises.md ADDED
@@ -0,0 +1,5 @@
1+# Exercises: lesson 4
2+
3+1. Time yourself: rewrite the example from memory in 10 minutes, then diff it against the original.
4+2. Add a check that prints a warning when a result looks wrong (negative, empty, too big).
5+3. Rewrite the main part so it runs twice with two different inputs, without copying the code.
+15-0lessons/04-loops-that-do-the-work/loops.py ADDED
@@ -0,0 +1,15 @@
1+# Sum up a shopping list
2+prices = [2.49, 0.99, 5.25, 3.10]
3+total = 0
4+
5+for price in prices:
6+ total += price
7+
8+print(f"Total: {total:.2f}") # Total: 11.83
9+
10+# Count down with while
11+seconds = 3
12+while seconds > 0:
13+ print(seconds, "...")
14+ seconds -= 1
15+print("Go!")