Exercise: Step Counter Functions
Write the four functions behind a step-counter app's reports, with parameters, return values, a default and a keyword argument, and let pytest's tests call each one to check what it returns.
Apprentice · Python · Updated
Stride, the step-counter app, has its reports designed and its numbers missing. You'll write the four functions behind them: one turns steps into miles, one works out the calories a walk burns, one says whether a goal was met, and one sums up a walker's week in a single line. From this exercise on, the tests call your functions directly and check what each one returns, the way Python programmers test their own code.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-python-step-counter, also linked from the Exercise box beside the lesson. Clone it into your python-course folder. In VS Code's terminal:
cd ~/python-course
git clone https://github.com/ask-codey/exercise-python-step-counter.git
Git makes a folder, exercise-python-step-counter, with the exercise code inside. Get the Exercise Code with Git walks through cloning step by step, if you'd like a reminder. Open that folder in VS Code (File > Open Folder), open its terminal with Terminal > New Terminal, and make the exercise's virtual environment, the same three steps as in the exercises before. On Windows (PowerShell):
python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt
On a Mac (zsh):
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
The prompt starts with (.venv) once the virtual environment is on. The exercise's README says what each command does, and what to do if PowerShell won't run the second one.
The folder holds:
steps.py: the four functions, for you to finish.week.py: prints a week's report with your functions, so you can see them at work. Ready to use.tests/test_steps.py: the tests, which call each function and check what comes back. You don't need to change it.requirements.txtandpytest.ini: the package the tests need, and where pytest finds the tests and your code.
The tests and week.py bring your functions in with an import line, such as from steps import steps_to_miles. import and the Standard Library explains how that works, in Module 3.
What to do
Every function in steps.py is already declared with its name and parameters (summary_line needs one more, in step 4), and for now it stops with a NotImplementedError that says Not built yet. The docstring under each one says what it should return, with examples:
def steps_to_miles(steps):
"""Return how many miles the steps make: 2,000 steps make a mile.
Round the miles to 2 decimal places.
steps_to_miles(5000) -> 2.5
steps_to_miles(7300) -> 3.65
steps_to_miles(4321) -> 2.16 (2.1605, rounded)
"""
raise NotImplementedError("Not built yet.")
In each function, replace the raise NotImplementedError("Not built yet.") line with your own code:
steps_to_miles(steps)returns the miles, rounded to 2 decimal places.calories_burned(miles, weight)returns the calories a walk burns, rounded to a whole number. Stride's rule of thumb: each mile burns 0.6 calories for every pound the walker weighs.met_goal(steps, goal)returnsTruewhen the steps reach the goal (reaching it exactly counts), andFalsewhen they don't.summary_line(name, steps)needs one more parameter first:goal, the week's step goal, which defaults to70000. Then it returns one line, such as"Maya: 52000 steps, 26.0 miles, 18000 to go", or"Leo: 74000 steps, 37.0 miles, goal met!"when the goal is reached. Its docstring gives three examples, one withgoal=40000as a keyword argument.
Each function returns its answer. None of them prints anything: the tests read what comes back.
Check your work
Run the tests from the exercise folder, with the virtual environment on:
pytest
At first all 15 tests fail. That's expected: each function you finish turns its tests green. A failing test's message says what it called, what it expected and what came back, for example steps_to_miles(5000) should return 2.5, but it returned 2500.0.
To see your functions at work, run python week.py. It prints the week's report from your four functions.
Hints
- Work an example through by hand first. 7,300 steps divided by 2,000 steps a mile is 3.65 miles.
round(number, 2)rounds to two decimal places andround(number)to a whole number. - Return, don't print. A function that prints its answer hands back
None, and the test's message says so. Parameters and Return Values shows the difference. - A comparison is already an answer.
steps >= goalisTrueorFalseon its own, somet_goalcan return it as it is. - Give
goalits default in thedefline, after the parameters that have none, as Default and Keyword Arguments shows. Then a call can leave it out, give it by position, or give it by name. - Let one function call another.
summary_lineneeds the miles and whether the goal was met, and the functions above it already work both out. An f-string puts the pieces in their places.
When you're done
Every test is green: pytest's last line says 15 passed. And python week.py prints:
Stride: this week's walkers
Maya: 52000 steps, 26.0 miles, 18000 to go
Leo: 74000 steps, 37.0 miles, goal met!
Sam: 41000 steps, 20.5 miles, goal met!
Maya's Saturday walk: 3.65 miles, 328 calories
Did Leo reach 10000 steps on Monday? True
Now look back at summary_line. If Stride changed to 2,200 steps a mile, how many of your functions would you have to change? If summary_line works out the miles itself instead of calling steps_to_miles, the answer is two.
Submit your exercise
The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. Hand In Your Work with Git walks through each step.
On GitHub, click +, then New repository. Name it (for example
exercise-python-step-counter), choose Public, and click Create repository. Leave out the README: your folder already has one.In VS Code's terminal, in your exercise folder, send your work to your new repo. Point your copy at your own repo, then commit and push:
git remote set-url origin https://github.com/your-name/exercise-python-step-counter.git git add . git commit -m "My solution" git push -u origin mainPaste your repo's address, such as
https://github.com/your-name/exercise-python-step-counter, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.
Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.
So what
Small functions that take values in and hand answers back are what every real program is made of, and tests that call them one at a time are how you know each piece works before you put them together.