A production-ready console-based trivia game that fetches questions from the Open Trivia Database API and provides an interactive Q&A experience.
This application follows enterprise-grade best practices:
- Comprehensive logging with timestamps and severity levels
- Structured debug information for troubleshooting
- User-friendly console feedback
- Input validation and error handling throughout
- Graceful degradation on API failures
- Type hints for code clarity and IDE support
- Automatic retry logic with exponential backoff (configurable)
- Timeout handling for network requests
- Connection error recovery
- HTML entity decoding for proper question/answer display
- Answer verification and immediate feedback
- Score tracking and detailed results
- Modular architecture (Client, Game, UI)
- Easy to extend (add new question sources, themes, etc.)
- Configuration constants at the top for quick adjustments
pip install -r requirements.txtpython trivia_app.py- ✓ 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
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 = 1trivia_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 ApplicationAdd 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 questionCustomize UI:
class ColoredConsoleUI(ConsoleUI):
# Add colored output, themes, etc.- requests - HTTP library for API calls (only external dependency)
The application handles:
- Network timeouts and connection errors
- Invalid API responses
- User input validation
- Graceful game interruption (Ctrl+C)
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- Leaderboard/score persistence
- Multiple game modes (timed, survival, etc.)
- Difficulty filtering
- Category selection
- Database integration
- Web UI variant
- API metrics collection