Module · Detail

Functions with return (Calorie Check)

This module introduces functions with return value and connects string processing with numerical calculation. Learners structure a multi-step calculation process and implement it in Python in a way that is easy to follow.

Time 10 minutes
Language Python 3.2 (SE)
Tasks 1
Preview image: Functions with return (Calorie Check)

Introduction (Original excerpt)

Kaya Code snapshot is in front of a series of food items. Each one stores its values as text – for example "800/240". The first value represents an amount, the second represents a property of this food item.

Didactic classification

Subjective objective

At its core is the implementation of a function with return value. Learners process structured text data, split it with split() and convert the individual components with float() to numerical values. Based on this, a given mathematical formula is modeled and correctly implemented.

Input analysis is required to structure a multi-step algorithm: analyze input, adjust data types, perform calculation, and provide result using return. The connection between model (calorie calculation formula) and implementation highlights the role of functions as self-contained, reusable computation units.

Competency development

Didactic added value in teaching

Flow of the lesson unit

1

Calculate calories

A function is defined, which accepts a text parameter. The text is broken down into two components, converted to numbers, and processed according to the given formula. The result is returned and output in the main program. anzahl_kalorien defines a function that accepts a text parameter. The text is split into two components, converted to numbers and processed according to the given formula. The result is returned and displayed in the main program.

  • Didactic focus: Structuring a calculation algorithm with return value
  • Typical challenge: Correct data type conversion and order of calculation steps

Work assignment (excerpt)

  1. Define the function anzahl_kalorien with a parameter.
  2. Split the given text with split("/") into two values.
  3. Convert both values with float to numbers.
  4. Calculate kilocalories (amount × energy density ÷ 1000) and return the result with return .

Example (excerpt)

1
2
3
4
5
def anzahl_kalorien(angaben):
    a, b = angaben.split("/")
    a = float(a)
    b = float(b)
    return a * b / 1000

This excerpt illustrates the complete sequence of splitting, type conversion, calculation, and return within a clearly structured function.

Hints for classroom practice

This module connects the modeling of a formula with its step-by-step implementation in a clearly structured function.

The clearly formulated task structure supports transparent lesson organization and facilitates targeted result verification. Differentiation is possible through further calculations or additional input formats.

Request a demo access and test the module in your own course context to systematically solidify functions with return values.