Member-only story

How to Create a World Capitals Quiz Game in Python

Adina Socaci
11 min readAug 20, 2024

--

In this tutorial, we’ll walk through building a simple yet fun world capitals quiz game in Python.

Version 1

Prerequisites

Before we begin, make sure you have Python installed on your computer. We’ll also use a few libraries that you can install via pip if you don’t have them already:

pip install requests pandas

Step 1: Fetching the Data

We’ll use the requests library to fetch data from an online source. There are various APIs available for country and capital information; one such source is the REST Countries API.

import requests

def fetch_capitals():
url = "https://restcountries.com/v3.1/all"
response = requests.get(url)
countries = response.json()

capitals = {}
for country in countries:
name = country.get("name", {}).get("common")
capital = country.get("capital", [None])[0]
if name and capital:
capitals[name] = capital

return capitals

This function fetches country data and extracts the common name and capital of each country, storing them in a dictionary.

Step 2: Setting Up the Quiz

--

--

Adina Socaci
Adina Socaci

Written by Adina Socaci

Tech | Science | Languages | Having Fun

No responses yet