Airport Code Lookup Check

airport-signIn this challenge we will implement a small Python program to:

  • Ask the user to enter a 3-letter airport code (e.g. LHR) for one of the top 20 busiest airports in the world.
  • Output the full name of the airport matching the code.

For this program we will use the official codes from the International Air Transport Association (IATA).

To make our program more robust, we will implement a couple of validation checks used to check if an airport code is valid. Our validation routine will:

  • Automatically convert the user input (airport code) to uppercase
  • Ensure the airport code provided is exactly 3 characters long (Length Check)
  • Ensure the airport code provided is one of the top 20 airport codes (Lookup Check)

To implement our lookup check we will use a dictionary data structure containing all 20 airport codes and their full names.

A dictionary is a data structure which contains an unordered list of key/value pairs. In our examples the keys are the airport codes, the values are the full airport names. e.g.

Notice the use of curly brackets in Python when creating a dictionary data structure.

With this dictionary we can then retrieve a single value by providing a key. e.g.

The above line of code would output “Dubai International Airport” on screen.

We can also check if a key exists in a dictionary by using the keyword in. e.g.

Python Code


Check the code below to validate a 3-letter airport code using both a length check and a lookup check.

Share Button
Tagged with: