45 lines
930 B
Markdown
45 lines
930 B
Markdown
---
|
|
type: challenge
|
|
title: "The Formatter"
|
|
xp: 50
|
|
duration: 25
|
|
difficulty: 2
|
|
---
|
|
|
|
# The Formatter
|
|
|
|
> **[INCOMING — Mission Control, Earth]**
|
|
>
|
|
> Cadet, raw numbers like `9.99` print fine, but `29.970000000000002`
|
|
> doesn't. Real programs *format* numbers. f-strings can do it inline:
|
|
>
|
|
> ```python
|
|
> price = 9.99
|
|
> f"${price:.2f}" # → "$9.99"
|
|
> ```
|
|
>
|
|
> The `:.2f` is a *format spec*: two decimal places, fixed-point.
|
|
>
|
|
> Implement `format_receipt(price, qty)` that returns a 3-line string:
|
|
>
|
|
> ```
|
|
> Item price: $<price>
|
|
> Quantity: <qty>
|
|
> Total: $<price * qty>
|
|
> ```
|
|
>
|
|
> Both prices use 2-decimal formatting. Use `\n` to join lines.
|
|
>
|
|
> [END TRANSMISSION]
|
|
|
|
## Your Task
|
|
|
|
Open `starter/starter.py`. Compute `total`, then return a single
|
|
f-string with `\n` separators.
|
|
|
|
## Objectives
|
|
|
|
- `format_receipt(9.99, 3)` returns the exact 3-line string
|
|
- Prices format to 2 decimal places
|
|
- Works for any positive `price` and `qty`
|