seed: curriculum content
This commit is contained in:
37
1.solar-system/2.first-light/06.the-translator/index.md
Normal file
37
1.solar-system/2.first-light/06.the-translator/index.md
Normal file
@@ -0,0 +1,37 @@
|
||||
---
|
||||
type: challenge
|
||||
title: "The Translator"
|
||||
xp: 50
|
||||
duration: 25
|
||||
difficulty: 2
|
||||
---
|
||||
|
||||
# The Translator
|
||||
|
||||
> **[INCOMING — Mission Control, Earth]**
|
||||
>
|
||||
> Cadet, when input comes in as text (a `str`), you can't do math
|
||||
> with it directly. You have to *translate* it to a number.
|
||||
>
|
||||
> Three converters:
|
||||
>
|
||||
> - `int("42")` → `42`
|
||||
> - `float("3.14")` → `3.14`
|
||||
> - `str(42)` → `"42"`
|
||||
>
|
||||
> Implement `years_until(age)` that returns `100 - age` as an `int`.
|
||||
> The argument may arrive as either `int` or `str` — handle both
|
||||
> by calling `int()` on it first.
|
||||
>
|
||||
> [END TRANSMISSION]
|
||||
|
||||
## Your Task
|
||||
|
||||
Open `starter/starter.py`. Convert `age` with `int()`, then return
|
||||
`100 - age`.
|
||||
|
||||
## Objectives
|
||||
|
||||
- `years_until(25)` returns `75`
|
||||
- `years_until("25")` also returns `75` (handles string input)
|
||||
- Works for any integer age 0–100
|
||||
@@ -0,0 +1,11 @@
|
||||
def years_until(age):
|
||||
"""Return how many years remain until age 100.
|
||||
|
||||
The argument may arrive as an int OR as a string (like "25"),
|
||||
so convert it to int first.
|
||||
|
||||
years_until(25) -> 75
|
||||
years_until("7") -> 93
|
||||
years_until(60) -> 40
|
||||
"""
|
||||
pass
|
||||
@@ -0,0 +1,21 @@
|
||||
from solution import years_until
|
||||
|
||||
|
||||
def test_int_input_25():
|
||||
assert years_until(25) == 75
|
||||
|
||||
|
||||
def test_int_input_7():
|
||||
assert years_until(7) == 93
|
||||
|
||||
|
||||
def test_int_input_60():
|
||||
assert years_until(60) == 40
|
||||
|
||||
|
||||
def test_string_input_converted():
|
||||
assert years_until("25") == 75
|
||||
|
||||
|
||||
def test_zero():
|
||||
assert years_until(0) == 100
|
||||
Reference in New Issue
Block a user