The request

From Laura Bennett, Head of Finance
Your goal
Step 1 of 3. Define parse_price(text) that strips spaces, removes '$' and ',' and returns a float. Set result to [parse_price(t) for t in raw_prices].
- Store a list of 4 in
resultnot done yet
Step 2 of 3. Each invoice line is (product name, quantity, price text). Set result to the invoice total as a float: the sum over lines of quantity times the parsed price, rounded to 2 decimals.
- Store a number in
resultnot done yet
Step 3 of 3. Set result to a list of strings, one per invoice line in order, in the exact form Walnut Dining Table x1 = $1,249.00: name, a space, x and the quantity, ' = $', then the line total (quantity times parsed price) with a thousands separator and 2 decimals.
- Store a list of 3 in
resultnot done yet
Step 1 · your code
Loading the editor…
Reference solution
Mote checks your result
Press Run. Each row that matches the goal becomes a firefly in your lantern.
Step 2 · your code
Loading the editor…
Reference solution
Mote checks your result
Press Run. Each row that matches the goal becomes a firefly in your lantern.
Step 3 · your code
Loading the editor…
Reference solution
Mote checks your result
Press Run. Each row that matches the goal becomes a firefly in your lantern.
What Laura gets, and why
The lesson
Text that should be numbers
Exports from invoicing tools, spreadsheets and web pages often store numbers as formatted text:
"$1,249.00", " 34.50", "12,000". You can’t add those up. float("$1,249.00") fails with
ValueError: could not convert string to float. Before any analysis, the text has to become a
number, and the cleanest way to do that is a small, well-tested function.
Step 1: one job, one function
def parse_price(text):
cleaned = text.strip().replace("$", "").replace(",", "")
return float(cleaned)
Each method returns a new string, so the chain reads left to right: trim the spaces, drop the dollar
sign, drop the thousands separators, then convert. Test it on the awkward cases, like a leading
space or a five-digit amount, before trusting it. If a value can’t be parsed (say "N/A"),
float() raises an error, and that’s usually what you want: a loud failure beats a silently wrong
total.
Step 2: functions inside expressions
Once parse_price exists, the total is one expression. Each invoice line is a tuple of three values,
which you can unpack right in the comprehension:
sum(qty * parse_price(price) for name, qty, price in invoice)
This is composition: small functions used as building blocks inside bigger expressions. Don’t
copy the .strip().replace(...) chain into the total. If the export format changes (a currency
code appears, say), you want to fix exactly one place.
The name name isn’t used in the total, but unpacking needs a variable for every position.
Python convention is to call an unused variable _, as in for _, qty, price in invoice.
Step 3: formatting for people
Analysts produce numbers for people, and people read $1,249.00 far more easily than 1249.0. The
format spec ,.2f adds a thousands separator and fixes two decimals:
f"{1249.0:,.2f}" # '1,249.00'
f"{138.0:,.2f}" # '138.00'
Keep the number as a number until the very last moment, then format. A list of formatted strings is great for an email, but you can’t sum it any more.
A small helper like receipt_line(name, qty, price_text) keeps the comprehension short and gives
the formatting rule a name. That pays off the day someone asks for the same line in a different
report.
Habits worth keeping
- Parse once, at the edge of your code, then work with clean numbers.
- Keep raw and cleaned values separate until you’ve checked the cleaning.
- Cross-check: the line totals from step 3, parsed back to numbers, should add up to the step 2 total.
Common mistakes
- Removing the comma but not the dollar sign (or the other way round), so
float()fails. - Forgetting
.strip().float(" 34.50")actually tolerates surrounding spaces, but other cleaning steps such as checkingstartswith("$")do not, so strip first. - Using
{line_total:.2f}without the comma, which prints1249.00instead of1,249.00.
Lantern lit!
Practised: functionsstring methodsfloat conversionf stringscomposition
- Solved
- No hints
- First run
