A fun and interactive 20 Questions game built with Python, OpenAI API, and Streamlit. The AI tries to guess an English word you're thinking of by asking intelligent yes/no questions.
- AI-powered question generation using OpenAI GPT
- Smart guessing based on your answers
- Track questions asked (up to 20, with extension to 25 if needed)
- Full conversation history
- Clean and user-friendly Streamlit interface
- Easy game restart functionality
- Python 3.8 or higher
- OpenAI API key (Get one here)
-
Clone or download this repository
-
Create a virtual environment (recommended):
python -m venv venv
-
Activate the virtual environment:
- On Windows:
venv\Scripts\activate
- On macOS/Linux:
source venv/bin/activate
- On Windows:
-
Install dependencies:
pip install -r requirements.txt
-
Set up your OpenAI API key:
- Create a
.envfile in the project root directory - Add the following line to
.env:OPENAI_API_KEY=your_openai_api_key_here - Replace
your_openai_api_key_herewith your actual OpenAI API key - Alternatively, you can copy
env_template.txtto.envand edit it:(On macOS/Linux:copy env_template.txt .env
cp env_template.txt .env)
- Create a
-
Make sure your virtual environment is activated
-
Run the Streamlit app:
streamlit run app.py
-
Open your browser to the URL shown in the terminal (usually
http://localhost:8501)
-
Push your code to GitHub (make sure
.envis in.gitignore- never commit API keys!) -
Go to Streamlit Cloud and connect your repository
-
Set up your OpenAI API key:
- In your Streamlit Cloud app dashboard, go to Settings → Secrets
- Add the following secret:
OPENAI_API_KEY=your_openai_api_key_here - Replace
your_openai_api_key_herewith your actual OpenAI API key - Click Save
-
Deploy your app - Streamlit Cloud will automatically detect your
app.pyand deploy it
Note: The .env file is only used for local development. On Streamlit Cloud, use the Secrets feature to securely store your API key.
- Click "🔄 New Game" or "🚀 Start Game" to begin
- Think of an English word (any noun, verb, adjective, etc.)
- The AI will ask you yes/no questions
- Answer using:
- Yes ✅ - The answer is yes
- No ❌ - The answer is no
- Sometimes 🤷 - It depends or sometimes true
- Unknown ❓ - You're not sure
- The AI will try to guess your word within 20 questions
- If the AI makes a guess, confirm if it's correct!
20Q/
├── app.py # Main Streamlit application
├── utils.py # Helper functions for OpenAI API and game logic
├── requirements.txt # Python dependencies
├── env_template.txt # Template for environment variables
├── .env # Your actual environment variables (create this)
└── README.md # This file
- No Database: All guessing logic comes from OpenAI's GPT model. The AI learns from your answers dynamically within each game session.
- Conversation History: Each question and answer is passed to the API, allowing the AI to build context and make smarter guesses.
- Smart Guessing: The AI analyzes patterns in your answers and makes educated guesses when confident.
- Question Limits: The game allows up to 20 questions by default, with an extension to 25 if needed.
- Make sure your
.envfile exists and containsOPENAI_API_KEY=your_actual_key - Verify your API key is valid and has credits on your OpenAI account
- Check that
python-dotenvis installed and loading correctly
- Ensure all dependencies are installed:
pip install -r requirements.txt - Make sure you're using the correct Python version (3.8+)
- Try running:
python -m streamlit run app.py - Check that Streamlit is installed:
pip show streamlit
Potential features for future versions:
- 🎯 Hint system
- 📊 Scoreboard or game history
- 🎨 Themes (animals, objects, movies)
- 🌍 Multiple language support
- 🏆 Difficulty levels
This project is open source and available for personal and educational use.
Built with:
- Streamlit - Web framework
- OpenAI API - AI question generation
- Python - Programming language
Enjoy playing 20Q! 🎉