← Course homeNext: Robotics motor lab →M16 · Python returns
Practical Python · M16

The Value Comes Back

Rover’s energy helper can calculate a value. Your job is to send that value back to the caller, store it, and display the result.

What’s saved: your code, predictions and answers clear when you leave. If you turn saving on, only which puzzles you finished and how much help you used is kept on this device. Test accounts also share it, and lesson timing, with the site’s maintainer.

A separate worked story shows real return and print events.
◇ Your mastery goalM16 · Returned values

Complete all 3 required tasks with a correct prediction, an inspected trace and an explanation that shows the concept. At least 2 must be independent checks, without answer-level help.

✓ Qualifying tasks: 0 / 3◎ Independent checks: 0 / 2 required
Read · Practice · Energy estimate

Rover’s battery station

0/3 checks · 0/2 independent transfers

Rover has 12 energy units and a trip costs 5. Build a helper that returns the energy left. Rover must keep the remaining energy on the dashboard.

Read the inputs

battery12 charge unitscost5 charge units spent

Mission: calculate the remaining charge by passing both values to energy_left.

Predict before you run

Follow one value through the helper, the assignment, and the final print. Write None when a call sends no value back. The final printed value may be None even if the helper displayed a number inside its body.

Build the helper

Write a function that returns the battery charge minus the cost. Call it with the mission’s values, assign the returned value to remaining, then print remaining.

Allowed here: one energy_left(charge, cost) function with one return or print statement, a call, an assignment to remaining, and print(remaining).

Help for this puzzleStart with a nudge. Open an example or solution only when you choose.

Highest help used here: none

For Further Reading and Exploration

These are outside sites with their own rules. You will see a notice before you leave. Pick one, not all.

  • Automate the Boring Stuff with Python

    Free book to read online. The functions chapter explains return values and what a function gives back.

    • Hands-on
    • May need an install
    • Ask an adult
  • CS50's Introduction to Programming with Python

    Free Harvard course with videos and notes. The first lecture shows functions that give back a value you can use.

    • Hands-on
    • Advanced
    • May count visits
  • MIT OpenCourseWare 6.100L

    Free college course notes and videos. It moves fast, so take one topic at a time. Look for the lectures on functions and what they return.

    • Hands-on
    • Advanced
    • May count visits
  • Raspberry Pi Foundation Projects

    Step-by-step Python projects. Pick one that uses functions and watch what each one returns. Some need hardware, so check the parts list.

    • Hands-on
    • May count visits