Session 3
3 lessons completed
Should Serve Drinks (Bartender)
CompletedAssignment
Complete should_serve_customer. Return True only if: customer_age >= 21, bartender is not on break, time is between 5 and 10 inclusive. The tips suggest inverting the logic — return False early for each failing condition.
Solution
def should_serve_customer(customer_age, on_break, time):
if customer_age < 21:
return False
elif on_break:
return False
elif time < 5 or time > 10:
return False
else:
return TrueConcept
Inverted logic (returning False early), elif, or operator, boolean simplification, not operator
How it went
Grasped inverted logic well. Initially wrote time < 5 > 10 instead of time < 5 or time > 10 — read it out loud, identified fix himself. Applied on_break simplification unprompted. Passed all 9 test cases.
Ratings
Key takeaways
- →Chained comparisons like `time < 5 > 10` don't work — write explicitly: `time < 5 or time > 10`
- →Simplify booleans: `on_break` not `on_break == True`, `not has_id` not `has_id == 0`
Mount Rental
CompletedAssignment
Complete check_mount_rental. If time_used >= time_purchased return 'overtime charged'. Otherwise return 'no charges yet'. Bonus: try without an else statement.
Solution
def check_mount_rental(time_used, time_purchased):
if time_used >= time_purchased:
return "overtime charged"
return "no charges yet"Concept
>= comparison, returning strings based on condition, indentation as structure, fallback return without else
How it went
Got comparison operator right first time. Had both return statements inside if block initially. Once explained using parent/child framing — 'the if is the child, the function is the parent' — understood immediately and fixed it. Final solution has no else.
Ratings
Key takeaways
- →Indentation is structure, not style. Ask: what lines should only run if this condition is true? Those get indented. Everything else stays at the parent level
Combat Advantage
Completed (struggled but got through independently)Assignment
Complete combat_evaluation. Set advantage=True if player_power > enemy_defense, evenly_matched=True if equal, otherwise disadvantage=True. Set the remaining variables to False.
Solution
def combat_evaluation(player_power, enemy_defense):
advantage, disadvantage, evenly_matched = False, False, False
if player_power > enemy_defense:
advantage = True
elif player_power == enemy_defense:
evenly_matched = True
else:
disadvantage = True
return advantage, disadvantage, evenly_matchedConcept
if/elif/else blocks, > and ==, setting variables inside conditionals, returning multiple values
How it went
Completed after struggling — the right outcome. Indentation clicked with parent/child framing. Initially wrote age > 18 instead of >= 18 and had string case error — both self-corrected after leading questions. Applied on_break simplification unprompted.
Ratings
Key takeaways
- →Watch > vs >= — at boundary values they produce different results. Double-check threshold conditions