seed: curriculum content
This commit is contained in:
42
1.solar-system/2.first-light/10.the-calculator/index.md
Normal file
42
1.solar-system/2.first-light/10.the-calculator/index.md
Normal file
@@ -0,0 +1,42 @@
|
||||
---
|
||||
type: challenge
|
||||
title: "The Calculator"
|
||||
xp: 100
|
||||
duration: 35
|
||||
difficulty: 2
|
||||
---
|
||||
|
||||
# The Calculator
|
||||
|
||||
> **[INCOMING — Mission Control, Earth]**
|
||||
>
|
||||
> Cadet, capstone. Combine everything you've learned in this module
|
||||
> into one program.
|
||||
>
|
||||
> Implement `calculate(a, b)` that returns a dictionary with all four
|
||||
> basic operations:
|
||||
>
|
||||
> ```python
|
||||
> {
|
||||
> "sum": a + b,
|
||||
> "difference": a - b,
|
||||
> "product": a * b,
|
||||
> "quotient": a / b,
|
||||
> }
|
||||
> ```
|
||||
>
|
||||
> Convert both inputs with `float()` so results are consistent — and
|
||||
> so string inputs work too.
|
||||
>
|
||||
> [END TRANSMISSION]
|
||||
|
||||
## Your Task
|
||||
|
||||
In `starter/starter.py`, convert both inputs to float, then return
|
||||
the dictionary with the four keys above.
|
||||
|
||||
## Objectives
|
||||
|
||||
- `calculate(10, 5)` returns the four operations as a dict
|
||||
- Works with int, float, and string inputs
|
||||
- All values are floats
|
||||
@@ -0,0 +1,15 @@
|
||||
def calculate(a, b):
|
||||
"""Return a dict with the four basic operations on (a, b).
|
||||
|
||||
Convert both inputs with float() so output is consistent.
|
||||
|
||||
Returned dict has these exact keys:
|
||||
"sum" — a + b
|
||||
"difference" — a - b
|
||||
"product" — a * b
|
||||
"quotient" — a / b
|
||||
|
||||
calculate(10, 5) -> {"sum": 15.0, "difference": 5.0,
|
||||
"product": 50.0, "quotient": 2.0}
|
||||
"""
|
||||
pass
|
||||
@@ -0,0 +1,34 @@
|
||||
from solution import calculate
|
||||
|
||||
|
||||
def test_returns_dict_with_four_keys():
|
||||
r = calculate(10, 5)
|
||||
assert set(r.keys()) == {"sum", "difference", "product", "quotient"}
|
||||
|
||||
|
||||
def test_basic_ten_five():
|
||||
r = calculate(10, 5)
|
||||
assert r["sum"] == 15.0
|
||||
assert r["difference"] == 5.0
|
||||
assert r["product"] == 50.0
|
||||
assert r["quotient"] == 2.0
|
||||
|
||||
|
||||
def test_seven_two():
|
||||
r = calculate(7, 2)
|
||||
assert r["sum"] == 9.0
|
||||
assert r["difference"] == 5.0
|
||||
assert r["product"] == 14.0
|
||||
assert r["quotient"] == 3.5
|
||||
|
||||
|
||||
def test_decimals():
|
||||
r = calculate(3.5, 1.5)
|
||||
assert r["sum"] == 5.0
|
||||
assert r["difference"] == 2.0
|
||||
assert r["product"] == 5.25
|
||||
|
||||
|
||||
def test_string_inputs_converted():
|
||||
r = calculate("10", "5")
|
||||
assert r["sum"] == 15.0
|
||||
Reference in New Issue
Block a user