Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fbrcode/trivia-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Trivia Q&A Application

A production-ready console-based trivia game that fetches questions from the Open Trivia Database API and provides an interactive Q&A experience.

Architecture & Design Principles

This application follows enterprise-grade best practices:

Observability

  • Comprehensive logging with timestamps and severity levels
  • Structured debug information for troubleshooting
  • User-friendly console feedback

Reliability

  • Input validation and error handling throughout
  • Graceful degradation on API failures
  • Type hints for code clarity and IDE support

Resilience

  • Automatic retry logic with exponential backoff (configurable)
  • Timeout handling for network requests
  • Connection error recovery

Accuracy

  • HTML entity decoding for proper question/answer display
  • Answer verification and immediate feedback
  • Score tracking and detailed results

Agility

  • Modular architecture (Client, Game, UI)
  • Easy to extend (add new question sources, themes, etc.)
  • Configuration constants at the top for quick adjustments

Installation

pip install -r requirements.txt

Usage

python trivia_app.py

Features

  • ✓ Fetches 10 random trivia questions from Open Trivia Database
  • ✓ Displays questions with difficulty and category
  • ✓ Presents multiple choice answers (randomized)
  • ✓ Immediate feedback on answer correctness
  • ✓ Final score report with question-by-question breakdown
  • ✓ Comprehensive error handling and logging
  • ✓ Clean, intuitive console UI

Configuration

Edit the constants at the top of trivia_app.py:

API_ENDPOINT = "https://opentdb.com/api.php?amount=10"
REQUEST_TIMEOUT = 10
MAX_RETRIES = 3
RETRY_DELAY = 1

Architecture Overview

trivia_app.py
├── Configuration & Constants
├── Logging Setup
├── Domain Models
│   ├── ResponseCode (Enum)
│   ├── TriviaQuestion (Question data)
│   └── TriviaResponse (API response)
├── API Client (Resilience)
│   └── TriviaAPIClient
├── Game Logic (Accuracy)
│   └── TriviaGame
├── Console UI (UX)
│   └── ConsoleUI
└── Main Application

Extension Points

Add a new question source:

class CustomTrivia(TriviaAPIClient):
    def __init__(self):
        super().__init__("https://your-api.com/trivia")

Add game modes:

class TimedGame(TriviaGame):
    # Add time limits per question

Customize UI:

class ColoredConsoleUI(ConsoleUI):
    # Add colored output, themes, etc.

Dependencies

  • requests - HTTP library for API calls (only external dependency)

Error Handling

The application handles:

  • Network timeouts and connection errors
  • Invalid API responses
  • User input validation
  • Graceful game interruption (Ctrl+C)

Logging Levels

Set the logging level in setup_logging():

setup_logging(level=logging.DEBUG)  # Verbose output
setup_logging(level=logging.INFO)   # Standard output
setup_logging(level=logging.WARNING) # Errors only

Future Enhancements

  • Leaderboard/score persistence
  • Multiple game modes (timed, survival, etc.)
  • Difficulty filtering
  • Category selection
  • Database integration
  • Web UI variant
  • API metrics collection

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages