Branching Logic & Decisions
Python conditionals (`if / elif / else`) and upgrading the console calculator to run the user-selected operation.
Learning Objectives
- Write boolean expressions and use comparison operators (
==,!=,<,<=,>,>=). - Control program flow with
if,elif, andelse. - Combine conditions with logical operators (
and,or,not) and understand truthiness. - Apply guard clauses (e.g., prevent division by zero) and provide simple user feedback.
Session Plan (theory)
- Recap (Week 1–2) Printing & formatting;
input()and basic casting. - Conditionals in Python
-
if / elif / elsestructure and execution order. - Comparison & logical operators; grouping with parentheses.
- Truthy/falsey values and common patterns (guard clauses, early returns with
exit()for scripts).
-
- Pattern examples
- Range checks, multi-branch decisions, simple validation messages.
Hands-on
- Console Calculator v2 (conditionals)
- Read two numbers (
float) and an operator from the user. - Use an
if/elif/elsechain to perform+,-,*,/. - Guard against division by zero and unknown operators.
- Print a clear, formatted result line.
- Read two numbers (