UK Postcodes – Distance Calculator



Your challenge is to write a Python program that asks the end-user to enter two valid UK postcodes and in return displays the distance in miles or km between those two postcodes.

To complete this challenge you will first find the exact longitude and latitude of both the postcodes entered by the end user. The following text file contains a list of all the UK outer codes (first part of a postcode) with their exact longitude and latitude.

UK Postcodes.csv

Once your program will have retrieved the longitude and latitude of both locations (postcodes) it will use the Haversine formula to calculate the exact distance between these two locations.

Haversine Formula

The Haversine formula is an equation important in navigation, giving great-circle distances between two points on a sphere from their longitudes and latitudes.

This formula will enable us to calculate the shortest distance over the earth’s surface – giving an ‘as-the-crow-flies’ distance between the two locations (ignoring any hills they fly over, of course!).

You can read more about the haversine formula on this wikipedia page.

To complete this challenge you will also need to investigate the use of trigonometric functions from the math library.




Note that when completing this challenge, longitudes and latitudes are given in degrees, not radians.

Once completed you can check your distances online.

Share Button
Posted in Computer Science, Python - Advanced, Python Challenges Tagged with: , ,

Our Latest Book