Quiz Application Python Project
Hello everyone, today we're going to make a fun Python quiz game.
What is the Mechanism Behind it?
Our quiz game will offer the player questions to which the player must respond with the correct answer. Each question will be attempted three times. If the player does not correctly answer the question after three tries, the game will go to the next question and the player will earn no points. However, if the player correctly answers the question, he will get one point. The total points scored by the player are shown at the conclusion of the game.
Setup of the Quiz Application Python Project
We'll need some questions and answers for our game before we start creating it.
Here how it looks:
quiz = {
1 : {
"question" : "What is the first name of Iron Man?",
"answer" : "Tony"
},
2 : {
"question" : "Who is called the god of lightning in Avengers?",
"answer" : "Thor"
},
3 : {
"question" : "Who carries a shield of American flag theme in Avengers?",
"answer" : "Captain America"
},
4 : {
"question" : "Which avenger is green in color?",
"answer" : "Hulk"
},
5 : {
"question" : "Which avenger can change it's size?",
"answer" : "AntMan"
},
6 : {
"question" : "Which Avenger is red in color and has mind stone?",
"answer" : "Vision"
}
}
In our situation, we'll employ some simple superhero-related questions.
Feel free to make up your own game questions and answers. Our questions and responses will be saved as a python dictionary in a separate python file.
Let's get started coding now.
Source Code for Quiz Application Python Project
from questions import quiz
def check_ans(question, ans, attempts, score):
"""
Takes the arguments, and confirms if the answer provided by user is correct.
Converts all answers to lower case to make sure the quiz is not case sensitive.
"""
if quiz[question]['answer'].lower() == ans.lower():
print(f"Correct Answer! \nYour score is {score + 1}!")
return True
else:
print(f"Wrong Answer :( \nYou have {attempts - 1} left! \nTry again...")
return False
def print_dictionary():
for question_id, ques_answer in quiz.items():
for key in ques_answer:
print(key + ':', ques_answer[key])
def intro_message():
"""
Introduces user to the quiz and rules, and takes an input from customer to start the quiz.
Returns true regardless of any key pressed.
"""
print("Welcome to this fun food quiz! \nAre you ready to test your knowledge about food?")
print("There are a total of 20 questions, you can skip a question anytime by typing 'skip'")
input("Press any key to start the fun ;) ")
return True
# python project.py
intro = intro_message()
while True:
score = 0
for question in quiz:
attempts = 3
while attempts > 0:
print(quiz[question]['question'])
answer = input("Enter Answer (To move to the next question, type 'skip') : ")
if answer == "skip":
break
check = check_ans(question, answer, attempts, score)
if check:
score += 1
break
attempts -= 1
break
print(f"Your final score is {score}!\n\n")
print("Want to know the correct answers? Please see them below! ;)\n")
print_dictionary()
print("Thanks for playing!")
Output: