Creating A Basic Trivia Game In Python Using APIs

Using Built-In Libraries to Build A Basic Geography Quiz

It’s the weekend! Congratulations. 🎉

Since I’m lazy on the weekends, I decided to do a more relaxed coding project than my typical data analysis and SQL queries.

Creating a basic trivia game in Python is a straightforward process, and it can be done using a variety of libraries and frameworks.

In this article, we will show how to create a trivia game using Python’s built-in libraries, such as requests and json.

First, we will start by importing the necessary libraries at the top of our script:

import jsonimport requests

These libraries will allow us to make HTTP requests to an API and parse the JSON responses.

Next, we will define the API endpoint for the trivia questions and the number of questions we want to retrieve.

API_URL = "https://opentdb.com/api.php?amount=50&category=22"

Opentdb.com is a website where we can generate up to 50 trivia questions in a variety of categories for free. For my example, I am using only geography questions. There is no authentication to retrieve the questions.

We will use the requests library to make a GET request to the API endpoint and retrieve the trivia questions.

response = requests.get(API_URL)data = json.loads(response.text)

Once we have retrieved the data, we will extract the questions and options from the JSON response. We will store the questions and options in a variable called questions.

questions = data["results"]

Now that we have pulled the questions from the JSON response, we can iterate through the questions list and present them to the user. The user can then select an answer and the program will check if the answer is correct.

correct_answers = 0for question in questions: print(question["question"]) print("Options:") for i in range(len(question["incorrect_answers"])): print(f"{i+1}. {question['incorrect_answers'][i]}") print(f"{len(question['incorrect_answers'])+1}. {question['correct_answer']}") user_answer = int(input("Enter the number of your answer: ")) if user_answer == len(question["incorrect_answers"]) + 1: print("Correct!") correct_answers += 1 else: print("Incorrect.")

Finally, we can display the user’s score to them.

print(f"You got {correct_answers} out of {len(questions)} questions correct.")

Results:

There’s a lot of ways you could take a project like this, such as adding UI, levels, advanced scoring, and whatever else your imagination can think of! There is a higher chance of errors during an advanced set up of an API, such as a user interface.

Conclusion

  • Creating a trivia game in Python is a relatively simple process that can be done using built-in libraries such as requests and json.

  • We used a trivia API to retrieve questions and options for the survey.

  • The program presents each question to the user, accepts input for an answer, and checks if the answer is correct.

  • The program keeps track of the number of correct answers and displays the final score to the user.

  • This is a basic example and can be customized to suit your needs, such as adding more functionality.

Thanks for reading!

Nick

P.S. Find the full code here on my github.

Remarks:

If you want to support me, please consider following on Medium and LinkedIn.

Not financial advice. You should seek a professional before making any financial decisions.

Reply

or to participate.