Python Programming Unit 2

Functions and Program Design

Turn working code into reusable, testable programs with clear structure.

This unit teaches students how to organize programs into meaningful functions. Students practice parameters, return values, scope, helper functions, decomposition, clear naming, preconditions, randomness, simulations, menu-driven programs, input validation, and simple expected-output checks. The goal is to help students stop writing one long script and start thinking like program designers.

Who This Unit Is For

Best for students who know Python basics and need cleaner structure before projects, simulations, AP CSP-style Create tasks, or data workflows.

Learning Goals

  • Write functions with useful parameters and return values.
  • Explain the difference between local variables and values passed into a function.
  • Break a larger program into helper functions that each do one job.
  • Use randomness and simulations responsibly with repeatable test examples when appropriate.
  • Create small expected-output checks before adding more features.

Key Concepts

What students practice in this unit

Functions are the bridge from beginner scripts to real projects. Once students can name a task, write a helper function, and test it separately, they can build larger programs without losing track of the logic.

Parameters and return values

Students learn how information enters a function, how a result comes back, and why printing is not the same as returning.

Decomposition

Students split a program into smaller helpers for validation, calculation, display, and repeated logic.

Scope

Students learn why a variable inside one function does not automatically exist everywhere else.

Testing habits

Students use small examples with known answers before trusting a larger simulation or menu program.

Practice

Exercises and mini-project ideas

These are public practice prompts students can use to strengthen the unit without exposing the full internal lesson sequence.

Practice Exercises

  • Write a dice simulator with a helper that rolls one die and another that rolls many times.
  • Create a quiz score calculator that drops the lowest score and returns the average.
  • Build reusable string helpers for initials, username cleanup, and title formatting.
  • Design a menu-based study app with functions for adding, listing, and reviewing terms.
  • Run a small simulation of coin flips and summarize the results.
  • Write expected-output checks for two helper functions before connecting the full program.

Mini-Project Ideas

  • Flashcard review app with menu choices and a score summary.
  • Simple event attendance simulator using random arrivals and counters.
  • Reusable text formatting toolkit for club announcements or study notes.

Common Student Mistakes

  • Printing inside a function when the program needs a returned value.
  • Changing a global variable instead of passing information clearly.
  • Writing helper functions that do too many unrelated jobs.
  • Using random output as the only test, which makes bugs harder to reproduce.
  • Forgetting to validate user input before using it in calculations.

Challenge Extension

Students add a testing section at the bottom of the file that calls each helper with at least three known inputs and explains the expected result in a short comment.

How This Prepares the Next Step

Students are ready to apply functions to lists, dictionaries, search tasks, sorting tasks, and larger data structures.

Related Code Scholars Paths

Ready to practice?

Build Python skills with a guided plan.

Students can use this page for review, then work with Code Scholars on targeted exercises, debugging support, projects, and next-step planning.