Module 9 β Build Simple AI Projects! π οΈ
intermediate25 XP
Build an AI Quiz Bot! π―
Create a Python quiz game with score tracking, hints, and encouraging messages
Build an AI Quiz Bot! π―
Your Quest: Build a Smart Quiz Game!
We'll build a quiz bot that:
- β Asks multiple-choice questions
- β Checks answers and keeps score
- β Gives hints when you're wrong
- β Cheers you on at the end!
π‘π‘ On this website, Python can't wait for you to type, so the player's answers come from a list. On your own computer you could use
input()instead.
Step 1: The question bank
Each question is a dictionary, and all of them go in a list:
Python
questions = [
{"question": "What does AI stand for?",
"options": ["A) Automatic Instructions", "B) Artificial Intelligence", "C) Amazing Internet"],
"answer": "B",
"hint": "Think: artificial (made by people) + intelligence (thinking)!"},
{"question": "What is training data used for?",
"options": ["A) Making computers faster", "B) Decorating apps", "C) Teaching AI with examples"],
"answer": "C",
"hint": "It's how AI learns, like showing a toddler lots of pictures!"},
{"question": "Which of these uses AI to forecast storms?",
"options": ["A) A weather app", "B) A light switch", "C) A pencil"],
"answer": "A",
"hint": "It looks at tons of past weather data to spot patterns."},
]
player_answers = ["B", "A", "A"]
score = 0
for i in range(len(questions)):
q = questions[i]
print("Q" + str(i + 1) + ":", q["question"])
if player_answers[i] == q["answer"]:
score = score + 1
print(" Correct!")
else:
print(" Not quite! Hint:", q["hint"])
print("Final score:", score, "out of", len(questions))Step 2: Cheer the player on π
Python
score = 2
total = 3
percent = score * 100 // total
if percent == 100:
print("PERFECT! You're an AI genius!")
elif percent >= 60:
print("Great work! You really know your AI!")
else:
print("Keep learning! You'll get there!")π‘ What makes this "AI-inspired"?
- A knowledge bank (a list of dictionaries), like the data AI systems store
- Checking answers and keeping score, like how AI measures its accuracy
- Hints when wrong, like the feedback AI uses to improve
π Challenge ideas
- Add more questions about AI topics you've learned.
- Add an easy/medium/hard level to each question.
- Use
random.shuffle(questions)to mix up the order each time!
β¨οΈ
Type it yourself
Don't just read it β type the example from above into the box, press Run βΆ, and watch what happens. Typing it yourself is how it really sticks! Stuck? Tap Show example.
Python Β· Playground
Output
Press βΆ Run to see your code come to lifeβ¦Quick check
01/4
In the quiz code, what does random.shuffle(questions) do?