Input validation

Published

2023-07-31

Input validation

Earlier, we wrote a Python program to convert kilograms to pounds. We trusted the user to provide a valid integer or float for weight in kilograms, and we did nothing to ensure that the information provided was reasonable. That is, we did not validate the input.

Validation of input is a crucial part of any program that accepts user input. Users sometimes provide invalid input. Some reasons for this include:

  • The prompt was insufficiently clear.
  • The user did not read the prompt carefully.
  • The user did not understand what kind of input is needed.
  • The user was being mischievous—trying to break the program.
  • The user made a typo or formatting error when entering data.

We would like our programs to respond gracefully to invalid input.

In this textbook, we’ll see several different ways to validate input and to respond to invalid input. The first that we’ll learn right now is simple bounds checking.

Bounds checking is an approach to input validation which ensures that a value is in some desired range. To return to our kilogram to pound conversion program, it does not make sense for the user to enter a negative value for the weight in kilograms. We might guard against this with bounds checking.

POUNDS_PER_KILOGRAM = 2.204623

kg = float(input('Enter weight in kilograms: '))
if kg >= 0:
    # convert kilograms to pounds and print result
else:
    print('Invalid input! '
          'Weight in kilograms must not be negative!')

So our program would perform the desired calculations if and only if the weight in kilograms entered by the user were non-negative (that is, greater than or equal to zero).

Here’s another example. Let’s say we’re writing a program that plays the game evens and odds. This is a two-player game where one player calls “even” or “odd”, and then the two players simultaneously reveal zero, one, two, three, four or five fingers. Then the sum is calculated and the caller wins if the sum agrees with their call.

In such a game, we’d want the user to enter an integer in the interval [0, 5]. Here’s how we might validate this input:

fingers = int(input('Enter a number of fingers [0, 5]: '))
if fingers >= 0 and fingers <= 5:
    # Generate a random integer in the range [0, 5], 
    # calculate sum, and report the winner.
else:
    print('Invalid input!')

Admittedly, these aren’t satisfactory solutions. Usually, when a user enters invalid data the program gives the user another chance, or chances, until valid data are supplied. We’ll see how to do this soon.

Nevertheless, simple bounds checking is a good start!

Comprehension check

  1. Can you use De Morgan’s Laws (see: Boolean expressions) to rewrite the bounds checking above?

  2. If we were to do this, would we be checking to see if fingers is in the desired range or outside the desired range?

  3. If your answer to 2 (above) was outside the desired range, how would you need to modify the program?

Original author: Clayton Cafiero < [given name] DOT [surname] AT uvm DOT edu >

No generative AI was used in producing this material. This was written the old-fashioned way.

This material is for free use under either the GNU Free Documentation License or the Creative Commons Attribution-ShareAlike 3.0 United States License (take your pick).