seed: curriculum content
This commit is contained in:
44
1.solar-system/2.first-light/08.the-formatter/index.md
Normal file
44
1.solar-system/2.first-light/08.the-formatter/index.md
Normal file
@@ -0,0 +1,44 @@
|
||||
---
|
||||
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`
|
||||
@@ -0,0 +1,13 @@
|
||||
def format_receipt(price, qty):
|
||||
"""Return a 3-line formatted receipt as a single string.
|
||||
|
||||
Format (note the dollar sign and 2-decimal places on prices):
|
||||
|
||||
Item price: $<price>
|
||||
Quantity: <qty>
|
||||
Total: $<price * qty>
|
||||
|
||||
format_receipt(9.99, 3) returns:
|
||||
"Item price: $9.99\nQuantity: 3\nTotal: $29.97"
|
||||
"""
|
||||
pass
|
||||
@@ -0,0 +1,13 @@
|
||||
from solution import format_receipt
|
||||
|
||||
|
||||
def test_basic():
|
||||
assert format_receipt(9.99, 3) == "Item price: $9.99\nQuantity: 3\nTotal: $29.97"
|
||||
|
||||
|
||||
def test_round_numbers():
|
||||
assert format_receipt(100, 1) == "Item price: $100.00\nQuantity: 1\nTotal: $100.00"
|
||||
|
||||
|
||||
def test_decimal_quantity_one():
|
||||
assert format_receipt(1.5, 10) == "Item price: $1.50\nQuantity: 10\nTotal: $15.00"
|
||||
Reference in New Issue
Block a user