What You Will Build
In the previous lesson, your chatbot could answer only one question. After giving the answer, the program stopped. In this lesson, you’ll improve the chatbot so that it keeps running. You can ask multiple questions without restarting the program. By the end of this lesson, your chatbot will work like this:
You: What is Python?
AI: Python is a programming language...
You: Who created Python?
AI: Guido van Rossum created Python...
You: exit
Goodbye!
This is a big improvement because the chatbot now behaves more like a real chat application.
Before You Start
Before continuing, make sure:
- Python is installed.
- The OpenAI Python SDK is installed.
- Your
app.pyfile from Lesson 12 is working. - Your OpenAI API key is ready.
Why Do We Need a Loop?
Think about a conversation with a friend. You don’t ask one question and walk away. You keep talking until one of you decides to end the conversation. A chatbot works the same way.
Instead of running only once, we’ll make our program repeat the same steps:
- Wait for a question.
- Send it to the AI.
- Show the answer.
- Wait for the next question.
Python gives us a simple way to repeat code using a while loop.
Step 1: Find This Line
Open your app.py file.
You’ll see this line from the previous lesson.
user_message = input("You: ")
Right now, it runs only once. We’re going to change that.
Step 2: Add a While Loop
Replace the single input line with this code.
while True:
user_message = input("You: ")
What does while True mean?
It tells Python:
“Keep running this code again and again until I tell you to stop.”
At this point, your program will keep asking for input forever. We’ll add a way to exit in the next step.
Step 3: Let the User Exit
Add these lines immediately after input().
if user_message.lower() == "exit":
print("Goodbye!")
break
How does this work?
If the user types:
exit
the program prints:
Goodbye!
and break stops the loop. Now the user can end the chatbot whenever they want.
Step 4: Move the AI Code Inside the Loop
Your chatbot should answer every question. So move the API request and the print() statement inside the while loop. Your program should now look like this:
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY"
)
while True:
user_message = input("You: ")
if user_message.lower() == "exit":
print("Goodbye!")
break
response = client.responses.create(
model="gpt-5",
input=user_message
)
print("\nAI:", response.output_text)
Try It Yourself
Run the program.
Ask a few questions.
You: What is HTML?
AI: HTML is the standard language used to create web pages.
You: Explain CSS.
AI: CSS is used to style web pages.
You: What is Python?
AI: Python is a beginner-friendly programming language.
You: exit
Goodbye!
Notice that the program doesn’t stop after the first answer.
What Changed?
Compared to the previous lesson, we added only two new ideas:
- A
whileloop to keep the chatbot running. - A
breakstatement to stop it when the user typesexit.
Everything else is exactly the same. That’s how real software is built—small improvements, one step at a time.
Common Errors
The chatbot exits immediately
Make sure you’re typing exit only when you want to close the program.
IndentationError
Everything inside the while loop must be indented by the same number of spaces.
VS Code usually does this automatically.
The chatbot answers only once
Check that the API request and print() statement are inside the while loop.
Key Points
- A
whileloop repeats the same code. breakstops the loop.- Your chatbot can now answer multiple questions.
- The project is becoming more useful with each lesson.
Mini Quiz
1. Which keyword repeats the code?
A. for
B. repeat
C. while
D. loop
Answer: C
2. Which keyword stops the loop?
A. stop
B. break
C. end
D. exit
Answer: B
3. What should the user type to close the chatbot in this lesson?
Answer: exit
4. Does the chatbot still answer only one question?
Answer: No.
5. What is the biggest improvement in this lesson?
A. Better design
B. Continuous conversation
C. Faster internet
D. New API key
Answer: B
Practice Exercise
Run your chatbot and ask at least five different questions.
For example:
- What is Java?
- Explain recursion.
- Give me three SQL interview questions.
- Suggest a Python project.
- Write a short motivational quote.
Finally, type:
exit
to close the chatbot.
Lesson Summary
Great job!
Your chatbot is no longer limited to a single question. It can now continue the conversation until the user decides to exit. This small change makes your project feel much more like a real chatbot.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
Shubhranshu Shekhar is a coding instructor, mentor, and founder of VSIT Delhi with 20+ years of teaching experience (since 2004). He has guided many students who are now working in multinational companies and specializes in Full Stack Development, Python, Digital Marketing, and Data Analytics.
