Case Study 3.2: Maya's Rate Calculator
The Context
Maya charges different rates depending on the type of work: - Standard consulting: $175/hour - Training delivery: $200/hour - Strategy workshops: $250/hour (fixed fee per day, roughly 8 hours) - Subcontracted work (she hires other consultants): $95/hour cost, billed at $145/hour
She also offers volume discounts: - 10+ hours/month per client: 5% discount - 20+ hours/month per client: 10% discount - Retainer clients (committed monthly hours): 8% discount
She manually calculates each invoice. It takes her 15–20 minutes per client per month, and she sometimes applies the wrong discount tier.
Python solution: a rate calculator that takes hours and project type as input variables and returns the correct invoice amount.
Version 1: The Basic Calculator
"""
maya_rate_calculator.py — Version 1
Simple rate calculator for Maya's consulting work
"""
# ── PROJECT INPUT ─────────────────────────────────────────────────────────────
client_name = "Hartwell Financial"
project_type = "consulting" # "consulting", "training", "workshop", "subcontract"
hours_this_month = 22.5
# ── RATE TABLE ────────────────────────────────────────────────────────────────
consulting_rate = 175.00
training_rate = 200.00
workshop_day_rate = 2000.00 # $250/hr × 8 hrs = $2,000/day
subcontract_billing_rate = 145.00
subcontract_cost_rate = 95.00
# ── DETERMINE BASE AMOUNT ─────────────────────────────────────────────────────
# For now, we handle all types; later chapters will make this cleaner
is_consulting = project_type == "consulting"
is_training = project_type == "training"
is_subcontract = project_type == "subcontract"
# Choose rate based on type
hourly_rate = consulting_rate if is_consulting else (
training_rate if is_training else
subcontract_billing_rate) # Simplified for now
base_amount = hours_this_month * hourly_rate
# ── DISCOUNT LOGIC ────────────────────────────────────────────────────────────
# Volume discount tiers (chapter 4 will make this cleaner with if/elif/else)
over_20_hours = hours_this_month >= 20
over_10_hours = hours_this_month >= 10
discount_rate = 0.10 if over_20_hours else (0.05 if over_10_hours else 0.00)
discount_amount = base_amount * discount_rate
invoice_amount = base_amount - discount_amount
# ── COST SIDE (for subcontracting) ────────────────────────────────────────────
cost_amount = hours_this_month * subcontract_cost_rate if is_subcontract else 0
gross_profit = invoice_amount - cost_amount
margin = gross_profit / invoice_amount if invoice_amount > 0 else 0
# ── OUTPUT ───────────────────────────────────────────────────────────────────
print(f"INVOICE SUMMARY — {client_name}")
print(f"{'='*45}")
print(f"Project type: {project_type.title()}")
print(f"Hours: {hours_this_month}")
print(f"Hourly rate: ${hourly_rate:.2f}")
print(f"Base amount: ${base_amount:,.2f}")
if discount_rate > 0:
print(f"Discount ({discount_rate:.0%}): -${discount_amount:,.2f}")
print(f"{'─'*45}")
print(f"Invoice total: ${invoice_amount:,.2f}")
if is_subcontract:
print(f"")
print(f"Internal economics:")
print(f" Subcontractor cost: ${cost_amount:,.2f}")
print(f" Gross profit: ${gross_profit:,.2f}")
print(f" Margin: {margin:.1%}")
Output for Hartwell Financial (22.5 consulting hours):
INVOICE SUMMARY — Hartwell Financial
=============================================
Project type: Consulting
Hours: 22.5
Hourly rate: $175.00
Base amount: $3,937.50
Discount (10%): -$393.75
─────────────────────────────────────────────
Invoice total: $3,543.75
The Discovery
Running this script, Maya realizes she's been applying discounts incorrectly. Her mental model was: 20+ hours gets 10%, 10–19 hours gets 5%. But she's been calculating the discount on the post-first-10-hours amount, not the full invoice. Python's logic is unambiguous — the discount applies to the whole invoice amount, which is what her contract says.
She estimates she's undercharged by 3–4% on about 40% of her invoices over the past year.
This is the "error reduction" value of Python made concrete. The calculation isn't complex — but doing it manually, invoice after invoice, month after month, introduces drift. The script is exact, every time.
Discussion Questions
-
The script uses nested ternary expressions for the discount logic. Is this readable? How would you rewrite it more clearly? (Chapter 4 will give you the tools.)
-
Maya discovers a calculation error she's been making manually. What does this reveal about the reliability of even simple repeated calculations done by hand?
-
The script has
if invoice_amount > 0 else 0in the margin calculation. Why is this guard necessary? What would happen without it? -
Maya currently inputs the variables at the top of the script manually before running it. How might she eventually want this to work instead? (Think about how she'd use this for 12 different clients.)