Skip to content
Computing · Secondary 3

Active learning ideas

Validating User Input in Programs

Active learning works well for validating user input because students learn best when they see their mistakes in real time. By testing code with deliberately bad inputs, students immediately grasp why validation matters and how to fix common errors.

MOE Syllabus OutcomesMOE: Programming - S3MOE: Cybersecurity - S3
25–45 minPairs → Whole Class4 activities

Activity 01

Plan-Do-Review30 min · Pairs

Pair Programming: Age Validator

Pairs write a program that prompts for age, checks if it's a positive integer between 0 and 150, and reprompts if invalid. Add custom error messages. Test with 10 sample inputs and log results.

Explain why programs need to check user input for validity.

Facilitation TipDuring Pair Programming, have students switch roles every 5 minutes to keep both engaged in writing and testing the validator.

What to look forProvide students with three scenarios: entering text into an age field, entering a 10-digit number into a phone number field, and entering an email address. Ask them to write one sentence for each scenario explaining why the input might be invalid and one line of Python code to check for a common invalid case.

RememberApplyAnalyzeSelf-ManagementDecision-MakingSelf-Awareness
Generate Complete Lesson

Activity 02

Plan-Do-Review45 min · Small Groups

Small Groups: Data Type Challenge

Groups create validators for three types: phone number (8 digits), email (contains @), and password (at least 8 characters). Share code via shared drive, then swap to break each other's validators.

Identify examples of invalid user input for different types of data (e.g., age, phone number).

Facilitation TipFor the Data Type Challenge, assign each group one data type to validate and then have them present their solution to the class.

What to look forPresent students with snippets of Python code that attempt to validate input. Ask them to identify any logical errors or missing checks in the code and explain how to correct them. For example, 'if input_age > 0:' misses the check for non-numeric input.

RememberApplyAnalyzeSelf-ManagementDecision-MakingSelf-Awareness
Generate Complete Lesson

Activity 03

Plan-Do-Review35 min · Whole Class

Whole Class: Input Bug Hunt

Display buggy code on projector. Class suggests invalid inputs to crash it, then votes on fixes. Code along to implement class-chosen validations.

Write simple code to check if user input meets basic requirements (e.g., is a number, is not empty).

Facilitation TipIn the Input Bug Hunt, provide broken examples with obvious flaws so all students can spot the issues without frustration.

What to look forPose the question: 'Imagine a program that asks for a user's username. What are at least three different ways a user could provide invalid input, and what security risks could arise if these inputs are not validated?' Facilitate a class discussion on their responses.

RememberApplyAnalyzeSelf-ManagementDecision-MakingSelf-Awareness
Generate Complete Lesson

Activity 04

Plan-Do-Review25 min · Individual

Individual: Retry Loop Creator

Students build a login simulator with username validation that loops until correct format. Submit screenshots of valid and invalid test runs.

Explain why programs need to check user input for validity.

Facilitation TipWhile students create the Retry Loop, circulate to ask guiding questions like, 'What happens if the user enters the same wrong input twice?'

What to look forProvide students with three scenarios: entering text into an age field, entering a 10-digit number into a phone number field, and entering an email address. Ask them to write one sentence for each scenario explaining why the input might be invalid and one line of Python code to check for a common invalid case.

RememberApplyAnalyzeSelf-ManagementDecision-MakingSelf-Awareness
Generate Complete Lesson

A few notes on teaching this unit

Teachers should emphasize that validation is a habit, not a one-time step. Avoid teaching validation as an afterthought by integrating it into every input example. Research shows that students retain concepts better when they debug their own mistakes, so provide time for trial and error rather than explaining solutions upfront.

Successful learning looks like students writing code that catches invalid inputs, such as rejecting letters in a phone number field or empty strings in a name field. Students should also explain why their checks work and how they prevent errors.


Watch Out for These Misconceptions

  • During Pair Programming: Age Validator, watch for students who assume all inputs will be correct.

    Have pairs intentionally break each other's code by entering invalid inputs like letters in the age field, then revise their conditionals to handle those cases.

  • During Data Type Challenge, watch for students who think validation is only for numbers.

    Assign groups to validate strings, emails, or dates, then have them demonstrate why their checks are necessary for those data types.

  • During Input Bug Hunt, watch for students who believe compiled code means inputs are handled safely.

    Provide buggy examples that compile but crash at runtime, then guide students to add validation checks to prevent these failures.


Methods used in this brief