World Capital Cities – Python Quiz

Are you ready to test your knowledge of world capital cities? In this Python programming challenge, we’ll create a fun and interactive quiz game. The game will display one capital city and four other non-capital cities in random order. Your task is to identify which city is the capital. If you guess correctly, you score a point and move on to the next question. The game ends when you guess incorrectly.

Using a List in Python

A list is a data structure used in python to store a collection of values. To complete this challenge we will use a list of cities which are not capital cities:

non_capital_cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix", 
   "Miami", "Toronto", "Vancouver", "Montreal", "Calgary",
   "Sydney", "Melbourne", "Brisbane", "Perth", "Adelaide",
   "Manchester", "Birmingham", "Liverpool", "Glasgow",
   "Marseille", "Lyon", "Toulouse", "Nice", "Nantes",
   "Hamburg", "Munich", "Cologne", "Frankfurt", "Stuttgart",
   "Milan", "Naples", "Turin", "Palermo", "Genoa",
   "Barcelona", "Valencia", "Seville", "Zaragoza", "Málaga",
   "Porto", "Funchal", "Braga", "Setúbal",
   "Thessaloniki", "Patras", "Larissa", "Heraklion", "Volos",
   "Yokohama", "Osaka", "Nagoya", "Sapporo", "Kobe",
   "Shanghai", "Guangzhou", "Shenzhen", "Chongqing", "Chengdu",
   "Mumbai", "Bangalore", "Hyderabad", "Ahmedabad", 
   "Rio de Janeiro", "São Paulo", "Salvador", "Fortaleza", "Belo Horizonte", 
   "Medellin", "Cali", "Barranquilla", "Cartagena",
   "Lima", "Arequipa", "Trujillo", "Chiclayo", "Iquitos",
   "Buenos Aires", "Córdoba", "Rosario", "Mendoza", "La Plata",
   "Auckland", "Christchurch", "Hamilton", "Tauranga",
   "Dubai", "Sharjah", "Al Ain", "Ajman", "Istanbul",
   "Izmir", "Bursa", "Antalya", "Jeddah", "Mecca", 
   "Medina", "Dammam", "Durban", "Johannesburg",
   "Mombasa", "Kisumu", "Nakuru", "Eldoret", "Lagos", 
   "Kano", "Ibadan", "Kaduna", "Port Harcourt", "Casablanca",
   "Fes", "Marrakech", "Tangier", "Accra", "Kumasi",
   "Tamale", "Sekondi-Takoradi", "Cape Coast", "Ho Chi Minh City", "Da Nang",
   "Hai Phong", "Can Tho", "Chiang Mai", "Pattaya", "Phuket",
   "Khon Kaen", "Penang", "Johor Bahru", "Ipoh", "Kuching",
    "Surabaya", "Bandung", "Medan", "Semarang"]

When using a list, we can access any value of the list using an index. The first value of the list, “New York” is at index 0.

print(non_capital_cities[0])  # New York
print(non_capital_cities[6])  # Toronto
print(non_capital_cities[18])  # Barcelona

The number of values stored in a list can be retrieved using the len() function:

number_of_cities = len(non_capital_cities)
print("There are " + str(number_of_cities) + " cities in this list.")

Using the random library in Python, we can randomly pick a value from the list. There are two ways this can be achieved:

import random

# Approach 1:
city = random.choice(non_capital_cities)
print(city)

# Approach 2:
index = random.randint(0,len(non_capital_cities)-1)
city = non_capital_cities[index]
print(city)

Using the random library you can also shuffle a list:

import random

cities = ["Paris", "Melbourne", "Istanbul", "Barcelona", "Kyoto"] 
random.shuffle(cities)
print(cities)

Finally, using a for loop you can access each value of a list one value at a time:

cities = ["Paris", "Melbourne", "Istanbul", "Barcelona", "Kyoto"] 

# Approach 1:
for city in cities:
   print(city)

# Approach 2:
for index in range(0, len(cities)):
   print(cities[index])

Using a Dictionary in Python

A dictionary also known as a hash table, is a data structure that stores a collection of key-value pairs. It is designed for efficient data retrieval, allowing values to be quickly accessed using their corresponding keys. Each key in a dictionary is unique and is associated with a specific value. To complete this quiz we will use a dictionary of capital cities where the keys will be the capital cities and the corresponding values will be the countries.

capital_cities = {
    "Paris": "France", "Berlin": "Germany", "Rome": "Italy",
    "Madrid": "Spain", "Lisbon": "Portugal", "Athens": "Greece",
    "Tokyo": "Japan",  "Beijing": "China", "New Delhi": "India",
    "Brasília": "Brazil", "Canberra": "Australia", "Ottawa": "Canada",
    "Washington, D.C.": "United States",  "Moscow": "Russia", "London": "United Kingdom",
    "Cairo": "Egypt", "Pretoria": "South Africa", "Nairobi": "Kenya",
    "Buenos Aires": "Argentina", "Lima": "Peru", "Oslo": "Norway",
    "Stockholm": "Sweden", "Helsinki": "Finland", "Copenhagen": "Denmark",
    "Wellington": "New Zealand", "Vienna": "Austria", "Brussels": "Belgium",
    "Amsterdam": "Netherlands", "Dublin": "Ireland", "Seoul": "South Korea",
    "Bangkok": "Thailand", "Kuala Lumpur": "Malaysia", "Jakarta": "Indonesia",
    "Singapore": "Singapore", "Ankara": "Turkey", "Riyadh": "Saudi Arabia",
    "Abu Dhabi": "United Arab Emirates", "Tehran": "Iran", "Baghdad": "Iraq",
    "Jerusalem": "Israel", "Mexico City": "Mexico", "Santiago": "Chile",
    "Bogotá": "Colombia", "Quito": "Ecuador", "Caracas": "Venezuela",
    "Sucre": "Bolivia", "Asunción": "Paraguay", "Montevideo": "Uruguay",
    "Georgetown": "Guyana", "Havana": "Cuba", "Santo Domingo": "Dominican Republic",
    "San Salvador": "El Salvador", "Guatemala City": "Guatemala", "Tegucigalpa": "Honduras",
    "Managua": "Nicaragua", "San José": "Costa Rica", "Panama City": "Panama",
    "Kingston": "Jamaica", "Nassau": "Bahamas", "Port-au-Prince": "Haiti",
    "Philipsburg": "Sint Maarten", "Reykjavik": "Iceland", "Warsaw": "Poland",
    "Budapest": "Hungary", "Bucharest": "Romania", "Sofia": "Bulgaria",
    "Zagreb": "Croatia", "Ljubljana": "Slovenia", "Bratislava": "Slovakia",
    "Prague": "Czech Republic", "Belgrade": "Serbia", "Sarajevo": "Bosnia and Herzegovina",
    "Podgorica": "Montenegro", "Pristina": "Kosovo", "Skopje": "North Macedonia",
    "Tirana": "Albania", "Valletta": "Malta", "Nicosia": "Cyprus",
    "Yerevan": "Armenia", "Baku": "Azerbaijan", "Tbilisi": "Georgia",
    "Kiev": "Ukraine", "Minsk": "Belarus", "Vilnius": "Lithuania",
    "Riga": "Latvia", "Tallinn": "Estonia", "Hanoi": "Vietnam",
    "Phnom Penh": "Cambodia", "Vientiane": "Laos", "Rangoon": "Myanmar",
    "Manila": "Philippines", "Kathmandu": "Nepal", "Colombo": "Sri Lanka",
    "Dhaka": "Bangladesh", "Thimphu": "Bhutan", "Male": "Maldives",
    "Islamabad": "Pakistan", "Kabul": "Afghanistan", "Tashkent": "Uzbekistan",
    "Dushanbe": "Tajikistan", "Ashgabat": "Turkmenistan", "Bishkek": "Kyrgyzstan",
    "Nur-Sultan": "Kazakhstan", "Ulaanbaatar": "Mongolia", "Pyongyang": "North Korea",
    "Bandar Seri Begawan": "Brunei", "Taipei": "Taiwan", "Hong Kong": "Hong Kong",
    "Macau": "Macau", "Canberra": "Australia", "Rabat":"Morocco"
}

Accessing a value from a dictionary is done by providing a key. For instance:

city = "Jakarta"
country = capital_cities[city]
print(city " is the capital city of " + country)

To access a random key from a dictionary, we can first create a list of all the available keys. The we can extract a random value from this list:

keys = list(capital_cities)
city = random.choice(keys)
country = capital_cities[city])
print(city " is the capital city of " + country)

Python Quiz

Your task is to use the two data structures provided in the code below to complete the quiz. You will need your program to:

    Randomly select a capital city from the dictionary of capital cities
    Randomly select four non capital cities from the list of cities
    Merge the selected capital city and the 4 non capital cities into a single list
    Shuffle the selected cities to present them in a random order
    Prompt the player to guess which of the 5 cities is a capital city
    Check if the player is correct and give some feedback either “Correct: Paris is the capital of France” or “Incorrect answer. The correct answer was Paris which is the capital of France”
    Implement a scoring system (1 point per correct answer) and inform the user of their score
    Keep asking questions and end the game when the player guesses incorrectly

Python Code

Here is the Python code with our two data structure.

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!