Add lesson 3: Making decisions with if
3 changed files with 38 additions and 0 deletions
lessons/03-making-decisions-with-if/README.md ADDED
| @@ -0,0 +1,19 @@ | ||
| 1 | +# Lesson 3: Making decisions with if | |
| 2 | + | |
| 3 | +Making decisions with if comes up in almost every real Python project. We start with the why, then write code together. | |
| 4 | + | |
| 5 | +Full lesson: [https://glibcode.net/courses/python-basics/lessons/3](https://glibcode.net/courses/python-basics/lessons/3) | |
| 6 | + | |
| 7 | +## Files | |
| 8 | + | |
| 9 | +- `grade.py`: if, elif and else on a test score | |
| 10 | + | |
| 11 | +## Run it | |
| 12 | + | |
| 13 | +```sh | |
| 14 | +python3 grade.py | |
| 15 | +``` | |
| 16 | + | |
| 17 | +## Exercises | |
| 18 | + | |
| 19 | +See [exercises.md](exercises.md). Ask in the forum once you've tried. | |
lessons/03-making-decisions-with-if/exercises.md ADDED
| @@ -0,0 +1,5 @@ | ||
| 1 | +# Exercises: lesson 3 | |
| 2 | + | |
| 3 | +1. Time yourself: rewrite the example from memory in 10 minutes, then diff it against the original. | |
| 4 | +2. Combine this lesson with the previous one: use something from both in a small program of your own. | |
| 5 | +3. Find one thing in `grade.py` you could name better and rename it everywhere. | |
lessons/03-making-decisions-with-if/grade.py ADDED
| @@ -0,0 +1,14 @@ | ||
| 1 | +score = int(input("Score (0-100): ")) | |
| 2 | + | |
| 3 | +if score >= 90: | |
| 4 | + grade = "A" | |
| 5 | +elif score >= 75: | |
| 6 | + grade = "B" | |
| 7 | +elif score >= 60: | |
| 8 | + grade = "C" | |
| 9 | +else: | |
| 10 | + grade = "try again" | |
| 11 | + | |
| 12 | +print("Your grade:", grade) | |
| 13 | +if score == 100: | |
| 14 | + print("Perfect score!") | |