Video
Python conditionals with Harvard CS50P
Watch the short lesson, then use three stock requests to test when if, elif and else each run.
This English short from Harvard CS50P explains conditional branches. Try the feed exercise first, then use three inputs to check your understanding.
What to watch for
- An if, elif and else chain checks from top to bottom and runs the first matching branch.
- Changing branch order can change the result.
- If two actions should both be possible, consider separate conditions.
Try a three-branch exercise
A barn has 7 kg. Print Enough if a request is smaller than stock, Empty after filling if it equals stock, and Wait if it is larger.
| Request | Expected output | What it tests |
|---|---|---|
| 5 | Enough | Below stock |
| 7 | Empty after filling | Equality |
| 9 | Wait | Above stock |
Then predict what happens when stock and request are both zero. A real problem that disallows zero requests needs to state that constraint.
Continue with several requests
The Python Pasture practice plan follows several requests and checks when stock changes. The official Harvard CS50P short includes subtitles and a transcript.