Creating a Python Script to Output Capitals of Countries
Python Weekend Techniques — Using Python to Output Capitals Out of Countries
Python is a versatile and widely-used programming language known for its simplicity, readability, and flexibility. With its clean and concise syntax, Python is a great choice for beginners and experienced developers alike.
Its extensive standard library and rich ecosystem of third-party packages enable it to excel in various domains, from web development and scientific computing to data analysis, artificial intelligence, and automation. Python’s cross-platform compatibility and active community make it an ideal tool for solving diverse challenges, allowing developers to write elegant and efficient code for a wide range of applications.
This article will be part of a limited series of using Python for fun stuff. Weekdays will be reserved for trading articles as usual and the weekends will discuss interesting Python topics.
Creating the Script
Countries and capitals are terms related to geography and governance. Here’s what they refer to. A country, also known as a nation or state, is a distinct political and geographic entity with defined borders, a government, and a population. Countries have their own laws, regulations, cultures, and often their own languages. They can vary in size from very small territories to large landmasses.
Countries can be independent sovereign entities recognized by the international community, or they might be dependencies, territories, or regions governed by another country. The capital of a country is the city that serves as the center of government, administration, and often culture. It’s where the main branches of government, such as the executive, legislative, and judicial branches, are located.
Capitals are typically chosen strategically and historically, and they play a crucial role in the country’s governance, decision-making, and representation on the international stage. In addition to government institutions, capitals often house cultural landmarks, historic sites, and important institutions like museums and universities.
Knowing the capitals of different countries is useful for understanding global geography, politics, and international relations.
Let’s code a simple Python script that asks the user for the name of the country and then outputs its capital:
import requests
def get_country_capital(country):
url = f"https://restcountries.com/v3.1/name/{country}?fullText=true"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
capital = data[0]['capital'][0]
return capital
else:
return "Capital not found"
def main():
while True:
country_name = input("Enter a country name (or 'x' to quit): ")
if country_name.lower() == "x":
print("Exiting the program.")
break
capital = get_country_capital(country_name)
print('--------')
print(f"The capital of {country_name.capitalize()} is {capital.capitalize()}.")
print('--------')
if __name__ == "__main__":
main()
Here’s an example of the code:
Enter a country name (or 'x' to quit): France
The output of the code is as follows:
--------
The capital of France is Paris.
--------
Here’s another example of the code:
Enter a country name (or 'x' to quit): Brazil
The output of the code is as follows:
--------
The capital of Brazil is Brasília.
--------
Are you interested in market sentiment and its strength to deliver powerful market forecasts? If so, then you can check out my other newsletter The Weekly Market Sentiment Report. It’s a weekly newsletter that highlights and explains the key market configurations using powerful market sentiment indicators and strategies!