Skip main navigation

Validating user input

Learn how to validate data using Python.

Data validation is the process of checking that data coming into a program is valid and appropriate and can be dealt with by your program.

Without data validation, programs may raise errors, not act in the way expected, and be open for abuse.

For example, when buying items online, the store will check that the number of items being purchased is:

  • A number
  • Not negative
  • Less than the stock available

If they didn’t check this, it could cause the store to stop working, order items which cannot be delivered, or create issues with their stock management software.

Data for important tasks cannot be instantly trusted; it needs validation to ensure that it is fit for purpose.

In the next two steps you will learn about how to validate data from users and file input.

Validating the data type

To ensure that data input from users is of the right type and appropriate, it needs to be validated.

In week one you created a maths game which asked the user to enter a number, using code similar to this:

number = int(input("Enter a number > "))

If you run this program and enter text and not a number, Python will raise a ValueError and the program will stop.

Mu IDE running the program above, with the text "Hello" entered in response and a `ValueError` exception shown

To validate that the data input by the user is of the correct type, you can use exception handling to capture the ValueError and continue. For example:

try:
number = int(input("Enter a number > "))
except ValueError:
print("This was not an integer")

While this approach would stop the program from raising an error and stopping, the user only has one opportunity to enter the correct value, and the program would still be likely to crash, as a valid number had not been captured. A more sophisticated solution is often needed that validates a user’s input and ensures that a correct value is entered before continuing.

Review the code below and predict what you think it will do:

def enter_an_integer():

number = input("Enter a number > ")
valid_integer = False

while not valid_integer:
try:
number = int(number)
valid_integer = True
except ValueError:
number = input("Invalid. Enter a number > ")

return number

valid_number = enter_an_integer()

print("Thank you")

Run the program and test it with the following scenarios:

  • Entering a valid number
  • Repeatedly entering an invalid number
  • Entering a decimal number/float e.g. 1.23

By creating a function which uses a while loop to continually test whether the user’s input is an integer, only valid integer numbers will be returned.

Using the same approach, can you create another function which validates that the user has entered a float?

Validating the data contents

After validating that a value is of the right data type, you may also need to check that the value is appropriate. This may include checking that the value input is within a range (for example, between 1 and 10) or one of a finite number of options (for example, months of the year). This type of validation is known as bounds or range checking.

In the example below, a user is asked to enter their age. Any values below 0 and above 117 are noted as invalid.

age = int(input("How old are you? "))

if age < 0 or age > 117:
print("That is not a valid age")

Validating someone’s age may be different depending on the scenario. It may be that the user must be over a certain age before they can use your program. Setting an upper age range is more complicated, as you don’t wish to penalise anyone for their age, but as higher numbers are entered, it becomes increasingly unlikely that the value is correct.

I used the upper value of 117, as there is no one alive older than 117 today. Validating above this very high age only benefits a small number of individuals, whereas it provide no protection for the larger number of 17-year-old’s who may accidentally entering the 117. Careful thought should be put into determining validation bounds.

It is also common to need to check that data input is one of a series of options, for example a day of the week. The program below uses a list to denote the possible options and then checks that the data input is one of those options:

days_of_the_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

day = input("What day is it? ")

if day not in days_of_the_week:
print("This is incorrect")

Note: The condition day not in days_of_the_week will return True if the value of day is not contained in the list days_of_the_week.

Validating a date

For this challenge, use the techniques described in this step to create a program that will prompt the user to enter:

  • The year
  • The month as a word (January to December)
  • The day in the month (1 to 31)

You program should:

  • Validate that the user input has the correct data types
  • Use range checking to ensure the month is valid and that the day in the month is between 1 and 31

As an optional challenge, consider how you would validate the day in the month based on the month entered.

Share your code in the comments section.

Remember to use three tilde characters (~~~) at the start and end of your code, as in this example comment text:

I created this program.

~~~
print("This is my code")
~~~

Refer to our ‘Sharing code on FutureLearn’ guide for more information.

This article is from the free online

Programming 103: Saving and Structuring Data

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now