Technologies K–10 · Years 7–8

Python functions tested against known values

Digital and Communication Technologies focus area (NSW Technology 7–8, 2023); Digital Technologies: Processes and production skills, Producing and implementing (ACARA v9)

Practical, model not builtLow risk

This site has no interactive model of its own. Where a step or a material names a Concept Studio model, simulation or tool, it has not been built; an external simulation a step names (for example PhET) is not part of this site.

The idea

A function packages a calculation so it can be tested against known values, and testing floating-point results needs a tolerance because decimals are stored in binary.

What you need

  • Computer with Python 3
  • Test table: input, expected output, actual output, pass or fail

How to do it

  1. Write def c_to_f(c): return c * 9 / 5 + 32 and def f_to_c(f): return (f - 32) * 5 / 9.
  2. Write tests with known values: c_to_f(0) is 32, c_to_f(100) is 212, c_to_f(-40) is -40, f_to_c(212) is 100.
  3. Add a test that c_to_f(36.6) == 97.88 and run it.
  4. Print c_to_f(36.6) to see the stored value, then change that test to math.isclose(c_to_f(36.6), 97.88).
  5. Add a round-trip test: f_to_c(c_to_f(x)) is close to x for x from −50 to 50.
  6. Use the functions in a program that reads a temperature and prints both scales.

What you should see

c_to_f(0) = 32.0, c_to_f(100) = 212.0, c_to_f(−40) = −40.0 and f_to_c(212) = 100.0 all pass. c_to_f(36.6) returns 97.88000000000001, so the exact-equality test fails although the function is right; math.isclose passes. f_to_c(100) returns 37.77777777777778. The round-trip tests pass with a tolerance. The learner knows it worked when every test in the table passes and they can explain why the exact test failed.

What changes

This activity lists no variables to change, measure and keep the same.

Common misconceptions

Each of these ideas is wrong, and the activity is a chance to test it.

  • If a calculation is correct the computer's answer is exactly the decimal answer (many decimals cannot be stored exactly in binary).
  • One passing test proves a function works (tests need normal, boundary and unusual values).
  • Functions make programs slower so they should be avoided (they make code reusable and testable).

Safety card

Low riskLearners carry it out

Hazards

  • None beyond normal computer use

Controls

  • Not applicable

Note

No chemicals or heat.

Curriculum references

The NSW syllabus outcomes and Australian Curriculum v9 codes this activity supports. They are references, not a verified or complete curriculum alignment.

  • Technology 7–8 Syllabus (2023), NESA. Current: taught from 2026. Code read from the outcomes page on 2026-09-22.TE4-DIG-02
  • Technology Mandatory 7–8 Syllabus (2017), NESA. Outgoing: replaced by the Technology 7–8 Syllabus (2023) from 2026 and not available after December 2027. Code read from the official syllabus document (DOCX) on 2026-09-22.TE4-4DP
  • Australian Curriculum v9AC9TDI8P09AC9TDI8P06

Sources

The pages the author read to write this activity.

  1. curriculum.nsw.edu.au/learning-areas/tas/technology-7-8-2023/outcomes
  2. www.nsw.gov.au/education-and-training/nesa/curriculum/tas/technology-mandatory-7-8-2017
  3. docs.python.org/3/tutorial/floatingpoint.html
  4. docs.python.org/3/library/math.html
  5. www.digitaltechnologieshub.edu.au/media/ywvisgk0/dt-challenge-7-8-python-intro-to-microbit-lesson-plan.pdf

All Concept Studio activities