Sai A Sai A
Updated date Mar 04, 2024
In this blog, we will learn how to create a basic quiz app using Python. Follow along as we guide you through setting up the environment, adding questions, implementing a timer, and more.

Introduction:

Python has become one of the most popular programming languages. With Python, you can create various applications, including games, web applications, and even educational tools like quiz apps. In this blog, we will learn the process of building a simple quiz app using Python

Setting Up the Environment

To begin, make sure you have Python installed on your computer. You can download and install Python from the official website (https://www.python.org/). Once Python is installed, you can proceed to create a new Python file for our quiz app.

# quiz_app.py

def main():
    # Initialize variables
    score = 0
    questions = [
        {"question": "What is the capital of France?", "answer": "Paris"},
        {"question": "Who wrote 'Romeo and Juliet'?", "answer": "William Shakespeare"},
        {"question": "What is the largest planet in our solar system?", "answer": "Jupiter"}
    ]

    # Display welcome message
    print("Welcome to the Simple Quiz App!")

    # Iterate through each question
    for question in questions:
        # Display the question
        print(question["question"])
        # Get user input
        user_answer = input("Your answer: ")
        # Check if the answer is correct
        if user_answer.lower() == question["answer"].lower():
            print("Correct!")
            score += 1
        else:
            print("Incorrect!")
    
    # Display final score
    print("Your final score is:", score)

if __name__ == "__main__":
    main()

Output:

Welcome to the Simple Quiz App!
What is the capital of France?
Your answer: Paris
Correct!
Who wrote 'Romeo and Juliet'?
Your answer: William Shakespeare
Correct!
What is the largest planet in our solar system?
Your answer: Mars
Incorrect!
Your final score is: 2

In this method, we set up the environment for our quiz app by creating a Python file named quiz_app.py. We define a main() function where we initialize variables such as the score and a list of questions with their respective answers. We then iterate through each question, displaying it to the user and prompting them to input their answer. After each answer, we check if it is correct and update the score accordingly. Finally, we display the user's final score.

Adding More Questions

To make our quiz app more engaging, let's add more questions to the list.

# quiz_app.py

def main():
    # Initialize variables
    score = 0
    questions = [
        {"question": "What is the capital of France?", "answer": "Paris"},
        {"question": "Who wrote 'Romeo and Juliet'?", "answer": "William Shakespeare"},
        {"question": "What is the largest planet in our solar system?", "answer": "Jupiter"},
        {"question": "Who painted the Mona Lisa?", "answer": "Leonardo da Vinci"},
        {"question": "What is the powerhouse of the cell?", "answer": "Mitochondria"}
    ]

    # Display welcome message
    print("Welcome to the Simple Quiz App!")

    # Iterate through each question
    for question in questions:
        # Display the question
        print(question["question"])
        # Get user input
        user_answer = input("Your answer: ")
        # Check if the answer is correct
        if user_answer.lower() == question["answer"].lower():
            print("Correct!")
            score += 1
        else:
            print("Incorrect!")
    
    # Display final score
    print("Your final score is:", score)

if __name__ == "__main__":
    main()

Output:

Welcome to the Simple Quiz App!
What is the capital of France?
Your answer: Paris
Correct!
Who wrote 'Romeo and Juliet'?
Your answer: William Shakespeare
Correct!
What is the largest planet in our solar system?
Your answer: Jupiter
Correct!
Who painted the Mona Lisa?
Your answer: Leonardo
Incorrect!
What is the powerhouse of the cell?
Your answer: Mitochondria
Correct!
Your final score is: 4

In this method, we have expanded our quiz app by adding more questions to the list. This increases the variety of topics covered in the quiz, making it more engaging for the user. The rest of the functionality remains the same as in Method 1.

Implementing a Timer

To add an extra challenge to our quiz app, let's implement a timer that limits the amount of time the user has to answer each question.

# quiz_app.py
import time

def main():
    # Initialize variables
    score = 0
    questions = [
        {"question": "What is the capital of France?", "answer": "Paris"},
        {"question": "Who wrote 'Romeo and Juliet'?", "answer": "William Shakespeare"},
        {"question": "What is the largest planet in our solar system?", "answer": "Jupiter"},
        {"question": "Who painted the Mona Lisa?", "answer": "Leonardo da Vinci"},
        {"question": "What is the powerhouse of the cell?", "answer": "Mitochondria"}
    ]

    # Display welcome message
    print("Welcome to the Simple Quiz App!")

    # Iterate through each question
    for question in questions:
        # Display the question
        print(question["question"])
        # Start the timer
        start_time = time.time()
        # Get user input
        user_answer = input("Your answer: ")
        # Stop the timer
        end_time = time.time()
        # Calculate the time taken
        time_taken = end_time - start_time
        # Check if the answer is correct and within time limit (10 seconds)
        if user_answer.lower() == question["answer"].lower() and time_taken <= 10:
            print("Correct!")
            score += 1
        elif time_taken > 10:
            print("Time's up!")
        else:
            print("Incorrect!")
    
    # Display final score
    print("Your final score is:", score)

if __name__ == "__main__":
    main()

Output:

Welcome to the Simple Quiz App!
What is the capital of France?
Your answer: Paris
Correct!
Who wrote 'Romeo and Juliet'?
Your answer: William Shakespeare
Correct!
What is the largest planet in our solar system?
Your answer: Jupiter
Correct!
Who painted the Mona Lisa?
Your answer: Leonardo da Vinci
Correct!
What is the powerhouse of the cell?
Your answer: Mitochondria
Correct!
Your final score is: 5

In this method, we have added a timer to each question using the time module in Python. We start the timer before displaying each question and stop it after the user provides their answer. We then calculate the time taken to answer the question and check if it is within the time limit (set to 10 seconds in this example). If the user answers correctly and within the time limit, their score is incremented; otherwise, they are informed that time's up or their answer is incorrect.

Conclusion:

In this blog, we have learned how to create a simple quiz app in Python. We started by setting up the environment and defining the basic structure of our app. We then expanded the functionality by adding more questions and implementing a timer to add an extra challenge.

Comments (0)

There are no comments. Be the first to comment!!!