🐸

The Twin Return

The Data Tinkererpython-data-tinkerer-16-the-twin-return
Reward: 110 XP
|

The Twin Return

After opening the return path, Hoppy keeps working at the craft bench. This time, it is not just calculating one light value. It is packing spark stones into tiny charms. When one packing run ends, the bench naturally leaves behind two things: how many full charms are ready, and how many spark stones are still left over.

This lesson builds one new reflex on top of the last one: a function can hand back two related results together. If both results belong to the same small job, you do not need two separate function calls or two unrelated code paths.

One small job can naturally produce two results

When you pack spark stones into fixed-size charms, you usually care about two things at the same time: how many full charms you made, and how many stones are still left. Those two answers come from the same packing task, so it feels natural for the function to return both of them together.

spark_stones = 9
stones_per_charm = 4


def split_charm_batch(spark_stones, stones_per_charm):
  ready_charms = spark_stones // stones_per_charm
  spare_stones = spark_stones % stones_per_charm
  return ready_charms, spare_stones


ready_charms, spare_stones = split_charm_batch(spark_stones, stones_per_charm)

print("Ready charms:", ready_charms)
print("Spare stones:", spare_stones)

This is not about a new heavy theory. It is about one practical move: after a function finishes one small job, it can hand back two related results together, and the outside code can receive them with two variables.

Return them together, receive them together

The most important idea today is not the sentence “a function can return multiple values” by itself. The real point is why that makes sense here: full charms and leftover stones are both products of the same batch. So you return them together with return ready_charms, spare_stones, and you receive them together with ready_charms, spare_stones = ....

1
Make the function hand back both results together

The starter already calculates ready_charms and spare_stones. Replace the print inside the function with return ready_charms, spare_stones.

2
Use two variables to receive both returned values

Call split_charm_batch(spark_stones, stones_per_charm) once, then receive the returned pair with ready_charms, spare_stones.

3
Run it and inspect the two output lines

After you run the code, you will see the finished charm count and the leftover stone count side by side. They came from the same single function call. That is the feeling of the twin return.

When is a twin return useful?

When a function finishes one small job and you naturally care about two related results from that same job, returning both together makes sense. The key idea is not “more values.” It is “related values.”

Suggested Solution
Expand
Solution:
spark_stones = 14
stones_per_charm = 4


def split_charm_batch(spark_stones, stones_per_charm):
  ready_charms = spark_stones // stones_per_charm
  spare_stones = spark_stones % stones_per_charm
  return ready_charms, spare_stones


ready_charms, spare_stones = split_charm_batch(spark_stones, stones_per_charm)

print("Ready charms:", ready_charms)
print("Spare stones:", spare_stones)
Advanced Tips
Want more? Click to expand

This lesson keeps a very narrow but useful boundary: one function can hand back two related results, and the outside code can receive them with two variables. We are not expanding into advanced unpacking or turning this into tuple theory, because the real goal today is the feeling that one small task can have two natural outputs.

Next, you will take one more step: sometimes a function does not just return one or two loose values. It can return one tidy little report.

Loading...
Terminal
Terminal
Ready to run...