has_account = True
password_ok = True
if has_account:
if password_ok:
print("Welcome")Line-by-line explanation
- Two facts are stored.
- The outer if first checks for an account.
- The inner if is indented inside the outer block.
- Welcome appears only when both checks succeed.
Use nesting when the second question only makes sense after the first. For two simple facts, and may be clearer.
Tiny Practice
Show “Discount applied” only for a member who also has a coupon.
Starter template
is_member = True
has_coupon = True
if is_member:
CHANGE_MELine-by-line explanation
- The member check is outer.
- The coupon check belongs inside it.
Hint
- Indent another
if. - Check
has_coupon. - Indent the print line twice.
Show Solution
is_member = True
has_coupon = True
if is_member:
if has_coupon:
print("Discount applied")Line-by-line explanation
- The outer check succeeds.
- The inner check then succeeds.
- The message is displayed.