Hey what's going, this is Amir from Bek Brace channel.
Today, I have got an interesting application build walkthrough for you, where we are going to build a CLI application to find two cities in 2 different (or not) countries, and check the distance between them and also how much time would it take you if you would go from Point A to Point B by: Walking, Airplane and Bus.
This is the tutorial:
The code begins by importing the necessary libraries: inquirer for user input, geopy for geocoding and distance calculation, and simple_chalk for colorful output. These libraries enhance the functionality and visual experience of our distance calculator.
Next, we declare variables to store emojis representing different modes of transportation. We have a walking emoji, a bus emoji, and an airplane emoji. These emojis will be displayed alongside the calculated distances.
We then prompt the user to enter the names of the first and second cities along with their respective countries. We use the inquirer library to create interactive prompts and store the user's input.
After receiving the city and country names, we utilize the geopy library to obtain the latitude and longitude coordinates for both locations. This is achieved by using the geocode function from the Nominatim geocoder, which takes the city and country names as input and returns the corresponding coordinates.
With the coordinates available, we proceed to calculate the distances between the two locations using the distance function from the geopy library. In this code, we calculate the distances for walking, bus travel, and airplane travel. However, it's important to note that the bus and airplane distances are currently calculated using the same formula as the walking distance. To obtain accurate distances for these modes of transport, additional information and formulas specific to buses and airplanes would be required.
Next, we calculate the estimated time required for each mode of transportation. We assume a walking speed of 5 km/h, a bus speed of 60 km/h, and an airplane speed of 800 km/h. By dividing the distances by their respective speeds, we can estimate the time taken for each mode of transportation.
Finally, we print the results to the console using colorful output. The distances, along with the corresponding travel times, are displayed for each mode of transportation.
Conclusion:
In this blog post, we explored a Python code snippet that enables the calculation of distances between two cities and countries. By utilizing the inquirer, geopy, and simple_chalk libraries, we created an interactive and visually appealing distance calculator. This code can be a starting point for developing more sophisticated travel calculators or incorporating additional features to improve accuracy. Feel free to modify and enhance the code to suit your specific requirements.
Hope you will enjoy the video.
You will find the source code and other stuff in the description.
Cheers.
Amir.
Top comments (2)
Thanks for making such amazing video
Thank you, Ebrima
Some comments may only be visible to logged-in visitors. Sign in to view all comments.