Quiz app using API data - Python project

In this video, we build a Python trivia game using the command line interface and a third-party API, the Open Trivia DB API.

Quiz app using API data - Python project
Quiz app using API data - Python project

Make a command line game using data from a third-party API

Quiz app using API data - Python project

Video summary

In this video, we build a Python trivia game using the command line interface and a third-party API, the Open Trivia DB API. Here's a summary of what the Python script does:

It imports the necessary modules: requests, html, and random.

The get_question_pool function is defined to retrieve a pool of trivia questions from an API. It takes two parameters: amount (the number of questions to retrieve) and category (the category of questions to retrieve). It constructs the API URL based on the parameters, sends a GET request using the requests module, and retrieves the JSON response. The function then returns the list of question results from the response.

The shuffle_choices function takes a list of choices and shuffles them randomly using the random.shuffle function from the random module. It returns the shuffled list of choices.

The print_choices function takes a list of choices and prints them in the console. It uses the html.unescape function from the html module to decode any HTML entities in the choice text.

The get_user_choice function prompts the user to enter the number of their choice in the console. It validates the input to ensure it is within the range of valid choices (1 to 4) and returns the user's choice minus 1 (to convert it to a zero-based index).

The play_game function is the main game loop. It takes the amount and category parameters, retrieves the question pool using the get_question_pool function, and iterates over each question. For each question, it prints the question text, prepares the choices by adding the correct answer to the list, shuffles the choices, prints them using the print_choices function, gets the user's choice using the get_user_choice function, compares the user's choice with the correct answer, and prints whether the answer is correct or incorrect.

Finally, the script calls the play_game function with the amount and category values set to 10 and 11, respectively, if the script is run directly (not imported as a module).

To summarize... this script fetches trivia questions from an API, presents them to the user in a randomized order, and allows the user to select their answer. It then provides feedback on whether the answer is correct or incorrect.

What is a command line game?

A command line game, also known as a text-based or console game, is a type of game that is played solely within a command line interface (CLI) or terminal window. It typically does not have a graphical user interface (GUI) and relies on text-based input and output.

In a command line game, the gameplay and interaction occur through textual prompts and responses. The game presents information, choices, and challenges to the player through text, and the player provides input by typing commands or selecting options using the keyboard.

Command line games can vary in complexity and genre, ranging from simple text adventures and puzzles to more intricate simulations and strategy games. They are often appreciated for their simplicity, portability, and the ability to run them in a terminal environment without the need for graphical resources.

Examples of command line games include text-based RPGs (role-playing games), interactive fiction games, card games, board game adaptations, and various puzzle games.

Python modules

We use a few key Python modules / packages in the video: the Python random module, the Python html package, and the Python requests module.

Python random module

The Python random module is a built-in module that provides functions for generating random numbers and making random choices. It is commonly used in games, simulations, and any application that requires randomness.

Here are some key features and functions of the random module:

Random Number Generation

The random module allows you to generate random numbers. The random() function returns a random float between 0 and 1. You can use other functions like randint(a, b) to generate random integers within a specified range, and uniform(a, b) to generate random floats within a specified range.

Random Choices

The module provides functions for making random choices from a sequence. The choice(seq) function returns a random element from a given sequence, such as a list or a string. The choices(seq, k) function returns a list of k random elements from the sequence with replacement.


The random module enables you to shuffle the elements in a sequence randomly. The shuffle(seq) function shuffles the elements in-place, altering the original sequence.

Random Sampling

The module offers functions for random sampling from a population. The sample(population, k) function returns a list of k unique random elements from the population without replacement.

Seed Generation

The random module allows you to generate random seeds for initializing the random number generator. The seed(a=None) function initializes the random number generator with a given seed. If no seed is provided, it uses the current system time.

By utilizing the functions provided by the random module, you can introduce randomness and create unpredictable behaviors in your Python programs. Whether you need to generate random numbers, make random choices, shuffle sequences, or sample from populations, the random module provides the necessary functionality to incorporate randomness into your applications.

Python html package

The html package includes modules such as html.parser, html.entities, and html.escape, which are used for HTML parsing, entity conversion, and escaping HTML special characters, respectively. These modules are primarily focused on handling HTML-related tasks and manipulating HTML strings.

Here is a brief overview of the mentioned modules:


This module provides an HTML parser, which can be used to parse HTML documents and extract information from them. It allows you to traverse the HTML tree structure, access elements, retrieve attribute values, and handle different types of HTML tags and entities.


This module defines a mapping between HTML entities (e.g., &, <, >) and their corresponding Unicode characters. It provides functions like html.escape() and html.unescape() to convert HTML entities to Unicode characters and vice versa.


This module offers functions to escape and unescape special characters in HTML strings. The html.escape() function replaces characters like <, >, &, and ", among others, with their corresponding HTML entity representations. This is useful when you want to display HTML content as plain text or insert it into an HTML document safely.

These modules from the html package can be helpful when working with HTML data in Python, such as parsing HTML documents, handling special characters, or manipulating HTML strings. However, it's important to note that for more complex HTML processing or web scraping tasks, you may need to use external libraries such as Beautiful Soup or lxml, which provide more advanced HTML parsing capabilities.

Python requests module

The requests module is a popular Python library that simplifies making HTTP requests from Python code. It provides a convenient way to send HTTP requests, handle responses, and interact with web services or APIs.

Here's a brief overview of the requests module:

Sending HTTP Requests

The requests module offers functions like requests.get(), requests.post(), requests.put(), requests.delete(), and more to send different types of HTTP requests. These functions allow you to specify the URL, headers, query parameters, request body, and other parameters necessary for the request.

Handling Responses

Once a request is sent, the requests module provides access to the response through a Response object. This object contains information about the response, such as the status code, headers, and content. You can access these details through properties like response.status_code, response.headers, and response.content.

Request Parameters

The requests module allows you to pass various parameters along with the request. These parameters include headers, query parameters, form data, JSON data, cookies, and more. You can provide these parameters as dictionaries or keyword arguments depending on the specific request method.


The requests module supports different types of authentication methods, such as basic authentication, digest authentication, and OAuth. You can provide authentication credentials as part of the request using the appropriate authentication mechanism.

Session Management

The requests module includes a Session object that allows you to persist certain parameters across multiple requests, such as cookies or authentication credentials. Using a session can help streamline the process of making multiple related requests to the same server.

Error Handling

The requests module provides built-in exception handling for common HTTP errors, such as connection errors, timeouts, or invalid responses. You can catch these exceptions and handle them accordingly to gracefully handle errors during the request.

The requests module is widely used for various web-related tasks in Python, such as interacting with RESTful APIs, scraping web content, performing HTTP-based file uploads, and more. Its simplicity and extensive functionality make it a popular choice among developers when working with HTTP requests in Python.

The teaser 👉 Quiz app using API data - Python project #shorts

Open Trivia DB

In the video, we build an app that calls a third-party API, Open Trivia DB, to get trivia questions and answers.

Open Trivia DB (Open Trivia Database) is a free and open API (Application Programming Interface) that provides access to a vast collection of trivia questions and answers. It allows developers to retrieve trivia questions across various categories, difficulty levels, and formats. The OpenTDB API is widely used in trivia apps, games, and websites to incorporate interactive and engaging trivia experiences.

Here are some key points about Open Trivia DB:

Question Categories

OpenTDB offers a wide range of question categories, including general knowledge, science, history, sports, entertainment, geography, and more. These categories allow developers to tailor the trivia questions to specific themes or interests.

Difficulty Levels

Questions in the OpenTDB database are categorized into different difficulty levels, such as easy, medium, and hard. This provides flexibility for developers to choose the appropriate level of challenge for their trivia game or app.

Question Formats

OpenTDB supports multiple question formats, including multiple choice, true/false, and open-ended questions. This variety allows developers to create diverse and engaging trivia experiences.

API Access

OpenTDB provides a simple and well-documented API that developers can use to retrieve trivia questions programmatically. Developers can make HTTP requests to the OpenTDB API, specifying parameters such as the number of questions, category, difficulty, and question format.

Response Format

The API response from OpenTDB is typically in JSON format, making it easy for developers to parse and extract the necessary information, such as the question text, answer choices, correct answer, and other relevant details.


OpenTDB is a community-driven project that relies on contributions from users worldwide. Users can submit new trivia questions to the database, ensuring that the collection continues to grow and stay up-to-date.


OpenTDB questions are released under the Creative Commons Attribution-ShareAlike 4.0 International (CC-BY-SA 4.0) license. This license allows developers to freely use and distribute the trivia questions in their projects while attributing the source.