In 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.
airports = {"ATL":"Hartsfield–Jackson Atlanta International Airport", "PEK":"Beijing Capital International Airport", "DXB":"Dubai International Airport", "LAX":"Los Angeles International Airport", ... }
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.
print(airports["DXB"])
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.
if "DXB" in airports: print(airports["DXB"]) else: print("Airport code not recognised")
Python Code
Check the code below to validate a 3-letter airport code using both a length check and a lookup check.
Your Task
Your task is to create another dictionary called airlines which will contains twelve of the main international airlines with their 2-letter codes as follows:
Airline Code | Airline |
AA | AMERICAN AIRLINES |
AC | AIR CANADA |
AF | AIR FRANCE |
AI | AIR INDIA |
BA | BRITISH AIRWAYS |
DL | DELTA AIR LINES |
CA | AIR CHINA |
JL | JAPAN AIRLINES |
MS | EGYPTAIR |
QF | QANTAS AIRWAYS |
SQ | SINGAPORE AIRLINES |
UA | UNITED AIRLINES |
Your program should ask what airlines the end-user is flying with (using a 2 letter code input) and use the airlines dictionary to validate the user input and retrieve and display the full name of the airline company.
Solution...
The solution for this challenge is available to full members!Find out how to become a member:
➤ Members' Area