The Quiz Game is an interactive, educational game where players answer multiple-choice questions from various topics. This Python implementation of the Quiz Game provides an engaging way to test your knowledge across different subjects. Players are presented with a series of questions, and they must choose the correct answer from four options. The game keeps track of the score, provides immediate feedback on the correctness of each answer, and uses text-to-speech functionality to read the questions and options aloud. This game is a great way to enhance your learning and have fun simultaneously.
Before running the quiz game, ensure that you have the following installed and set up:
By the end of this project, you should be able to:
Install Python:
Install Required Libraries:
Download the Script:
By following these steps, you will create an interactive quiz game that engages players, tests their knowledge across various topics, and provides immediate feedback and visual results.
import random
import seaborn as sns
import matplotlib.pyplot as plt
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.setProperty('rate', 225)
ques=["What is the capital of India?",
"Which river is considered the holiest river in India?",
"Who was the first Prime Minister of India?",
"Which Indian city is known as the 'Silicon Valley of India'?",
"Which festival is known as the 'Festival of Lights' in India?",
"Which planet is known as the Red Planet?",
"Who wrote the play 'Romeo and Juliet'?",
"What is the largest ocean on Earth?",
"What is the capital city of Japan?",
"Which element has the chemical symbol 'O'?",
"Who is known as the father of modern physics?",
"What is the smallest country in the world by land area?",
"Which continent is known as the 'Dark Continent'?",
"What is the main ingredient in traditional Japanese miso soup?",
"In what year did the Titanic sink?",
"Which country is known as the Land of the Rising Sun?",
"What is the hardest natural substance on Earth?",
"Which famous artist painted the Mona Lisa?",
"What is the chemical formula for water?",
"Who was the first President of the United States?"]
opt = [
["Mumbai", "Kolkata", "New Delhi", "Bangalore"],
["Yamuna", "Ganga", "Godavari", "Narmada"],
["Mahatma Gandhi", "Sardar Vallabhbhai Patel", "Jawaharlal Nehru", "Dr. B.R. Ambedkar"],
["Hyderabad", "Chennai", "Pune", "Bangalore"],
["Holi", "Diwali", "Eid", "Pongal"],
["Earth", "Mars", "Venus", "Jupiter"],
["William Shakespeare", "Charles Dickens", "Mark Twain", "Jane Austen"],
["Atlantic Ocean", "Indian Ocean", "Arctic Ocean", "Pacific Ocean"],
["Beijing", "Seoul", "Tokyo", "Bangkok"],
["Gold", "Oxygen", "Silver", "Hydrogen"],
["Isaac Newton", "Albert Einstein", "Galileo Galilei", "Niels Bohr"],
["Monaco", "San Marino", "Liechtenstein", "Vatican City"],
["Asia", "Africa", "South America", "Antarctica"],
["Tofu", "Seaweed", "Miso paste", "Rice"],
["1912", "1905", "1915", "1920"],
["China", "Japan", "South Korea", "Thailand"],
["Gold", "Diamond", "Iron", "Quartz"],
["Vincent van Gogh", "Pablo Picasso", "Leonardo da Vinci", "Michelangelo"],
["CO2", "H2O", "O2", "H2SO4"],
["Thomas Jefferson", "John Adams", "Abraham Lincoln", "George Washington"]
]
corr1 = [
"New Delhi", "Ganga", "Jawaharlal Nehru", "Bangalore", "Diwali", "Mars",
"William Shakespeare", "Pacific Ocean", "Tokyo", "Oxygen", "Albert Einstein",
"Vatican City", "Africa", "Miso paste", "1912", "Japan", "Diamond",
"Leonardo da Vinci", "H2O", "George Washington"
]
c_exc = [
"Hurray!! your answer is correct ",
"Well done!! your answer is correct",
"Excellent!!your answer is correct",
"Fantastic!!your answer is correct",
"Bravo!!you are doing great",
"You got it!!your answer is correct",
"Great job!!you are doing well",
"Perfect!!your answer is correct",
"Spot on!!your answer is correct",
]
ic_exc = [
"Oops!!your answer is incorrect",
"Oh no!!your answer is incorrect ",
"Not quite!!you are wrong",
"your answer is wrong.Better luck next time!!",
"your answer is Close, but not quite!!",
"Sorry, that's wrong!!",
"Unfortunately, your answer is wrong!!",
"your answer is wrong.Maybe next time!!"
]
def speak(text):
engine.say(text)
engine.runAndWait()
def new_ans(ans):
idx = ans-1
flag = True
if opt[a][idx]==corr1[a]: flag=True
else : flag=False
return flag
name=input("enter your name.\n")
speak(f"Hello {name} ! All the best for your quiz")
print("--------------------YOUR QUESTIONS ARE ON YOUR SCREEN--------------------")
idx=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
score=0
cc=0 #count for correct
cw=0 #count for incorrect
for i in range(10):
a=random.choice(idx)
idx.remove(a)
print(f"Q {i+1}: {ques[a]}\n")
speak(f"{ques[a]}")
random.shuffle(opt[a])
for j in range(4):
print(f"{j+1}: {opt[a][j]}")
speak(f"{j+1}: {opt[a][j]}")
speak("Enter your answer.")
ans=int(input("Enter your answer: "))
new_ans(ans)
if new_ans(ans):
score+=10
cc+=1
print(f"your answer is correct. +10\nScore is {score}\n")
#rm=random.choice(c_exc)
speak(f"{random.choice(c_exc)}")
speak(f"your score is {score}")
else:
score-=5
cw+=1
print(f"your answer is incorrect. -5\nScore is {score}\n")
#rm=random.choice(ic_exc)
speak(f"{random.choice(ic_exc)}")
speak(f"your score is {score}")
print("--------------------------------------------------------------------------------")
print(f"final score is:{score}/100")
print("--------------------Thanks for giving us your valuable time--------------------.")
print("--------------------I hope you enjoyed giving this quiz.--------------------")
speak(f"Your final score is {score} out of 100 marks.")
speak("Thanks for giving us your valuable time. I hope you enjoyed giving this quiz")
#visualising result
a=["correct","incorrect"]
b=[cc,cw]
plt.pie(b,labels=a, autopct='%.2f%%')
plt.show()
import random
import seaborn as sns
import matplotlib.pyplot as plt
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.setProperty('rate', 225)
ques=["What is the capital of India?",
"Which river is considered the holiest river in India?",
"Who was the first Prime Minister of India?",
"Which Indian city is known as the 'Silicon Valley of India'?",
"Which festival is known as the 'Festival of Lights' in India?",
"Which planet is known as the Red Planet?",
"Who wrote the play 'Romeo and Juliet'?",
"What is the largest ocean on Earth?",
"What is the capital city of Japan?",
"Which element has the chemical symbol 'O'?",
"Who is known as the father of modern physics?",
"What is the smallest country in the world by land area?",
"Which continent is known as the 'Dark Continent'?",
"What is the main ingredient in traditional Japanese miso soup?",
"In what year did the Titanic sink?",
"Which country is known as the Land of the Rising Sun?",
"What is the hardest natural substance on Earth?",
"Which famous artist painted the Mona Lisa?",
"What is the chemical formula for water?",
"Who was the first President of the United States?"]
opt = [
["Mumbai", "Kolkata", "New Delhi", "Bangalore"],
["Yamuna", "Ganga", "Godavari", "Narmada"],
["Mahatma Gandhi", "Sardar Vallabhbhai Patel", "Jawaharlal Nehru", "Dr. B.R. Ambedkar"],
["Hyderabad", "Chennai", "Pune", "Bangalore"],
["Holi", "Diwali", "Eid", "Pongal"],
["Earth", "Mars", "Venus", "Jupiter"],
["William Shakespeare", "Charles Dickens", "Mark Twain", "Jane Austen"],
["Atlantic Ocean", "Indian Ocean", "Arctic Ocean", "Pacific Ocean"],
["Beijing", "Seoul", "Tokyo", "Bangkok"],
["Gold", "Oxygen", "Silver", "Hydrogen"],
["Isaac Newton", "Albert Einstein", "Galileo Galilei", "Niels Bohr"],
["Monaco", "San Marino", "Liechtenstein", "Vatican City"],
["Asia", "Africa", "South America", "Antarctica"],
["Tofu", "Seaweed", "Miso paste", "Rice"],
["1912", "1905", "1915", "1920"],
["China", "Japan", "South Korea", "Thailand"],
["Gold", "Diamond", "Iron", "Quartz"],
["Vincent van Gogh", "Pablo Picasso", "Leonardo da Vinci", "Michelangelo"],
["CO2", "H2O", "O2", "H2SO4"],
["Thomas Jefferson", "John Adams", "Abraham Lincoln", "George Washington"]
]
corr1 = [
"New Delhi", "Ganga", "Jawaharlal Nehru", "Bangalore", "Diwali", "Mars",
"William Shakespeare", "Pacific Ocean", "Tokyo", "Oxygen", "Albert Einstein",
"Vatican City", "Africa", "Miso paste", "1912", "Japan", "Diamond",
"Leonardo da Vinci", "H2O", "George Washington"
]
c_exc = [
"Hurray!! your answer is correct ",
"Well done!! your answer is correct",
"Excellent!!your answer is correct",
"Fantastic!!your answer is correct",
"Bravo!!you are doing great",
"You got it!!your answer is correct",
"Great job!!you are doing well",
"Perfect!!your answer is correct",
"Spot on!!your answer is correct",
]
ic_exc = [
"Oops!!your answer is incorrect",
"Oh no!!your answer is incorrect ",
"Not quite!!you are wrong",
"your answer is wrong.Better luck next time!!",
"your answer is Close, but not quite!!",
"Sorry, that's wrong!!",
"Unfortunately, your answer is wrong!!",
"your answer is wrong.Maybe next time!!"
]
def speak(text):
engine.say(text)
engine.runAndWait()
def new_ans(ans):
idx = ans-1
flag = True
if opt[a][idx]==corr1[a]: flag=True
else : flag=False
return flag
name=input("enter your name.\n")
speak(f"Hello {name} ! All the best for your quiz")
print("--------------------YOUR QUESTIONS ARE ON YOUR SCREEN--------------------")
idx=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19]
score=0
cc=0 #count for correct
cw=0 #count for incorrect
for i in range(10):
a=random.choice(idx)
idx.remove(a)
print(f"Q {i+1}: {ques[a]}\n")
speak(f"{ques[a]}")
random.shuffle(opt[a])
for j in range(4):
print(f"{j+1}: {opt[a][j]}")
speak(f"{j+1}: {opt[a][j]}")
speak("Enter your answer.")
ans=int(input("Enter your answer: "))
new_ans(ans)
if new_ans(ans):
score+=10
cc+=1
print(f"your answer is correct. +10\nScore is {score}\n")
#rm=random.choice(c_exc)
speak(f"{random.choice(c_exc)}")
speak(f"your score is {score}")
else:
score-=5
cw+=1
print(f"your answer is incorrect. -5\nScore is {score}\n")
#rm=random.choice(ic_exc)
speak(f"{random.choice(ic_exc)}")
speak(f"your score is {score}")
print("--------------------------------------------------------------------------------")
print(f"final score is:{score}/100")
print("--------------------Thanks for giving us your valuable time--------------------.")
print("--------------------I hope you enjoyed giving this quiz.--------------------")
speak(f"Your final score is {score} out of 100 marks.")
speak("Thanks for giving us your valuable time. I hope you enjoyed giving this quiz")
#visualising result
a=["correct","incorrect"]
b=[cc,cw]
plt.pie(b,labels=a, autopct='%.2f%%')
plt.show()
INITIALIZATION:
• Import random module
• Import pyttsx3 module
• Import seaborn and matplotlib.pyplot modules
• Initialize text-to-speech engine
• Set voice properties for text-to-speech engine
• Define questions list with corresponding questions
• Define options list with multiple-choice options for each question
• Define correct answers list
DEFINE HELPER FUNCTIONS:
• Function speak(text):
o Use text-to-speech engine to say the given text
• Function new_ans(ans):
o Check if the selected option matches the correct answer for the current question
o Return True if correct, otherwise False
MAIN GAME LOOP:
• Prompt player to enter their name
• Use text-to-speech to greet the player and wish them good luck
• Initialize list of question indices
• Initialize score to 0
• Initialize count of correct answers (cc) to 0
• Initialize count of incorrect answers (cw) to 0
• Loop for a predetermined number of questions (e.g., 10):
o Randomly select an index from the list of question indices
o Remove the selected index from the list
o Display the current question and its options
o Use text-to-speech to read the question and options aloud
o Prompt player to enter their answer by selecting a number corresponding to an option
o Call new_ans(ans) to check if the answer is correct
o If answer is correct:
Increment score by 10
Increment count of correct answers (cc)
Provide positive feedback
Use text-to-speech to read feedback and current score
o If answer is incorrect:
Decrement score by 5
Increment count of incorrect answers (cw)
Provide negative feedback
Use text-to-speech to read feedback and current score
• After all questions are answered:
o Display final score
o Use text-to-speech to announce the final score
o Thank player for participating
• Visualize results:
o Create a pie chart showing percentage of correct and incorrect answers
o Display the pie chart
INITIALIZATION:
• Import random module
• Import pyttsx3 module
• Import seaborn and matplotlib.pyplot modules
• Initialize text-to-speech engine
• Set voice properties for text-to-speech engine
• Define questions list with corresponding questions
• Define options list with multiple-choice options for each question
• Define correct answers list
DEFINE HELPER FUNCTIONS:
• Function speak(text):
o Use text-to-speech engine to say the given text
• Function new_ans(ans):
o Check if the selected option matches the correct answer for the current question
o Return True if correct, otherwise False
MAIN GAME LOOP:
• Prompt player to enter their name
• Use text-to-speech to greet the player and wish them good luck
• Initialize list of question indices
• Initialize score to 0
• Initialize count of correct answers (cc) to 0
• Initialize count of incorrect answers (cw) to 0
• Loop for a predetermined number of questions (e.g., 10):
o Randomly select an index from the list of question indices
o Remove the selected index from the list
o Display the current question and its options
o Use text-to-speech to read the question and options aloud
o Prompt player to enter their answer by selecting a number corresponding to an option
o Call new_ans(ans) to check if the answer is correct
o If answer is correct:
Increment score by 10
Increment count of correct answers (cc)
Provide positive feedback
Use text-to-speech to read feedback and current score
o If answer is incorrect:
Decrement score by 5
Increment count of incorrect answers (cw)
Provide negative feedback
Use text-to-speech to read feedback and current score
• After all questions are answered:
o Display final score
o Use text-to-speech to announce the final score
o Thank player for participating
• Visualize results:
o Create a pie chart showing percentage of correct and incorrect answers
o Display the pie chart
Displays the pie chart to provide a visual summary of the player’s performance.
Challenge: Integrating text-to-speech functionality.
Challenge: Randomly selecting questions without repetition.
Challenge: Providing immediate feedback and updating the score.
Test the program by running it multiple times and ensuring:
Q1: Can I add more questions to the quiz?
Q2: How can I change the voice or speed of the text-to-speech engine?
Q3: What if I encounter an error with the text-to-speech library?
Q4: Can I change the scoring system?
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India