Gamify Your Python Journey

Build an AI Quiz Game in Simple Steps

In partnership with

Digital Brain by DALL-E

A Step-by-Step Guide for Beginners

Learn AI in 5 minutes a day.

The Rundown is the world’s largest AI newsletter, read by over 600,000 professionals from companies like Apple, OpenAI, NASA, Tesla, and more.

Their expert research team spends all day learning what’s new in AI and gives you ‘the rundown’ of the most important developments in one free email every morning.

The result? Readers not only keep up with the insane pace of AI but also learn why it actually matters.

Hey there, future Python master! Ready to dive into a fun and interactive coding project? Today, we’re going to create a simple AI quiz game using Python. This project is perfect for beginners and will help you learn essential programming concepts like loops, conditionals, and functions. Let’s get started and have some fun!

 

What You Will Learn

  • Dictionaries: How to store questions and answers.

  • Loops: How to ask questions repeatedly.

  • Conditionals: How to check answers and keep score.

  • Functions: How to organize your code into reusable blocks.

  • User Input: How to get and use input from the player.

Step-by-Step Guide to Creating an AI Quiz Game

 

Step 1: Set Up the Questions and Answers

First, we need a set of questions and answers. We’ll store these in a dictionary where the keys are the questions and the values are the correct answers.

 

# Dictionary of questions and answers about AI

quiz = {

    'What does AI stand for?': 'Artificial Intelligence',

    'Who is considered the father of AI?': 'John McCarthy',

    'What is the primary programming language used for AI?': 'Python',

    'What is the name of the computer that defeated the world chess champion Garry Kasparov in 1997?': 'Deep Blue',

    'What is the process of teaching a machine to recognize patterns and make decisions called?': 'Machine Learning'

}

 

Explanation:

We’ve created a dictionary called quiz that holds questions as keys and their corresponding correct answers as values.

Step 2: Initialize the Score

We’ll keep track of the player’s score as they answer the questions.

 

# Initialize the score

score = 0

 

Explanation:

We’ve initialized a variable score to zero. This will keep track of the number of correct answers.

Step 3: Ask the Questions

We’ll loop through each question in the dictionary, get the user’s answer, and check if it’s correct.

 

# Loop through the dictionary

for question, correct_answer in quiz.items():

    print(question)

    answer = input("Your answer: ").strip().title()

 

    if answer == correct_answer:

        print("Correct!")

        score += 1

    else:

        print(f"Wrong! The correct answer is {correct_answer}.")

    print()  # Add a blank line for better readability

 

Explanation:

  • Loop: We loop through each question in the dictionary using for question, correct_answer in quiz.items().

  • User Input: We get the player’s answer with input("Your answer: ") and format it using .strip().title() to match the case of the correct answer.

  • Check Answer: We compare the player’s answer to the correct answer and update the score if they match.

  • Feedback: We give feedback to the player, letting them know if they were correct or not.

Step 4: Calculate and Display the Score

After all the questions have been answered, we’ll calculate the score as a percentage and display it to the player.

 

# Calculate the score percentage

total_questions = len(quiz)

score_percentage = (score / total_questions) * 100

 

print(f"Your final score is {score} out of {total_questions} questions.")

print(f"That's {score_percentage:.2f}%. Well done!")

 

Explanation:

  • Total Questions: We get the total number of questions with len(quiz).

  • Score Percentage: We calculate the score percentage by dividing the score by the total number of questions and multiplying by 100.

  • Display Score: We print the final score and the percentage to the player.

Step 5: Putting It All Together

Here’s the complete code for our quiz game:

 

# Dictionary of questions and answers about AI

quiz = {

    'What does AI stand for?': 'Artificial Intelligence',

    'Who is considered the father of AI?': 'John McCarthy',

    'What is the primary programming language used for AI?': 'Python',

    'What is the name of the computer that defeated the world chess champion Garry Kasparov in 1997?': 'Deep Blue',

    'What is the process of teaching a machine to recognize patterns and make decisions called?': 'Machine Learning'

}

 

# Initialize the score

score = 0

 

# Loop through the dictionary

for question, correct_answer in quiz.items():

    print(question)

    answer = input("Your answer: ").strip().title()

 

    if answer == correct_answer:

        print("Correct!")

        score += 1

    else:

        print(f"Wrong! The correct answer is {correct_answer}.")

    print()  # Add a blank line for better readability

 

# Calculate the score percentage

total_questions = len(quiz)

score_percentage = (score / total_questions) * 100

 

print(f"Your final score is {score} out of {total_questions} questions.")

print(f"That's {score_percentage:.2f}%. Well done!")

 

A Sample Output

Conclusion

Congratulations! You’ve just created your very own AI quiz game in Python. This project has introduced you to using dictionaries, loops, conditionals, and user input—all essential skills for any budding programmer.

 

Recommendation and Inspiration

Pickle RoomsStories for Startups & Entrepreneurs

Ready for More Python Fun?

Subscribe to our newsletter now and get a free Python cheat sheet! Dive deeper into Python programming with more exciting projects and tutorials designed just for beginners.

Keep exploring, keep coding, and have a blast with Python!