Module 3 — Reusable Truth
Wrap work into functions so you write it once and trust it everywhere.
2 lessons · about 25 min · difficulty 3 of 5
1. Writing a function
`def` names a block of work that takes inputs and gives back a result.
Anything after `return` is sent back to whoever called the function.
A function with no return gives back `None`.
Think of it like: A stamped shop drawing: draw it once correctly, then every crew builds from the same sheet.
At NoblePort: NobleHub prices change orders through one function. Field app, portal, and the estimating agent all call it, which is why the customer's number and the invoice number always match.
Try it yourself: Write `change_order_total(hours, rate, materials)` that returns the full price, then call it twice with different numbers.
Common mistake: Printing inside a function instead of returning. A printed value cannot be reused by the next step.
Why it matters: One tested function beats the same formula pasted in nine places.
2. Handling what goes wrong
try / except keeps one bad row from stopping the whole run.
Put the risky work in `try:` and the recovery in `except:`.
Catch the specific error you expect, so real bugs still surface.
Think of it like: A safety net under the scaffold — the work continues, and you know exactly who fell.
At NoblePort: The glossary importer processes hundreds of rows. One blank difficulty cell must not kill the upload, so each row is converted inside try/except and bad rows are reported back to the uploader.
Try it yourself: Feed `to_hours` the values "7.5", "", and "eight". Print each result and note which one you would want flagged to a human.
Common mistake: A bare `except:` that hides real bugs. Catch the specific error you expect, like ValueError.
Why it matters: Imported spreadsheets always contain one weird cell. This is how you survive it.