True/False Quiz Game Project using Python | 100 Days of Code | AJ Blogs

 



Data Code: (You can any questions and answers you want.)

question_data = [
    {"text": "A slug's blood is green.", "answer": "True"},
    {"text": "The loudest animal is the African Elephant.", "answer": "False"},
    {"text": "Approximately one quarter of human bones are in the feet.", "answer": "True"},
    {"text": "The total surface area of a human lungs is the size of a football pitch.", "answer": "True"},
    {"text": "In West Virginia, USA, if you accidentally hit an animal with your car, you are free to take it home to eat.", "answer": "True"},
    {"text": "In London, UK, if you happen to die in the House of Parliament, you are entitled to a state funeral.", "answer": "False"},
    {"text": "It is illegal to pee in the Ocean in Portugal.", "answer": "True"},
    {"text": "You can lead a cow down stairs but not up stairs.", "answer": "False"},
    {"text": "Google was originally called 'Backrub'.", "answer": "True"},
    {"text": "Buzz Aldrin's mother's maiden name was 'Moon'.", "answer": "True"},
    {"text": "No piece of square dry paper can be folded in half more than 7 times.",
        "answer": "False"},
    {"text": "A few ounces of chocolate can to kill a small dog.", "answer": "True"}
]

Quiz_Model Code:

class Question:
    def __init__(self, q_text, q_answer):
        self.text = q_text
        self.answer = q_answer

Quiz_Brain Code:

class QuizBrain:
    def __init__(self, q_list):
        self.question_number = 0
        self.score = 0
        self.question_list = q_list
    
    def still_has_questions(self):
        return self.question_number < len(self.question_list)
    
    def next_question(self):
        current_question = self.question_list[self.question_number]
        self.question_number += 1
        user_answer = input(f"Q.{self.question_number}: {current_question.text} (True/False): ")
        self.check_answer(user_answer, current_question.answer)
        
    def check_answer(self, user_answer, correct_answer):
        if user_answer.lower() == correct_answer.lower():
            self.score += 1
            print("You got it right")
        else:
            print("That's wrong.")
        print(f"The correct answer was: {correct_answer}")
        print(f"Your current score is: {self.score}/{self.question_number}")
        print("\n")
    

Main Code:

from question_model import Question
from data import question_data
from quiz_brain import QuizBrain


question_bank = []

for question in question_data:
    question_text = question["text"]
    question_answer = question["answer"]
    new_question = Question(question_text, question_answer)
    question_bank.append(new_question)

quiz = QuizBrain(question_bank)

while quiz.still_has_questions():
    quiz.next_question()
    
print("You've completed the quiz")
print(f"Your final score was: {quiz.score}/{quiz.question_number}")


Output:

Q.1: A slug's blood is green. (True/False): True
You got it right
The correct answer was: True
Your current score is: 1/1


Q.2: The loudest animal is the African Elephant. (True/False): False
You got it right
The correct answer was: False
Your current score is: 2/2


Q.3: Approximately one quarter of human bones are in the feet. (True/False): True
You got it right
The correct answer was: True
Your current score is: 3/3


Q.4: The total surface area of a human lungs is the size of a football pitch. (True/False): False
That's wrong.
The correct answer was: True
Your current score is: 3/4


Q.5: In West Virginia, USA, if you accidentally hit an animal with your car, you are free to take it home to eat. (True/False): False
That's wrong.
The correct answer was: True
Your current score is: 3/5


Q.6: In London, UK, if you happen to die in the House of Parliament, you are entitled to a state funeral. (True/False): True
That's wrong.
The correct answer was: False
Your current score is: 3/6


Q.7: It is illegal to pee in the Ocean in Portugal. (True/False): False
That's wrong.
The correct answer was: True
Your current score is: 3/7


Q.8: You can lead a cow down stairs but not up stairs. (True/False): True
That's wrong.
The correct answer was: False
Your current score is: 3/8


Q.9: Google was originally called 'Backrub'. (True/False): False
That's wrong.
The correct answer was: True
Your current score is: 3/9


Q.10: Buzz Aldrin's mother's maiden name was 'Moon'. (True/False): True
You got it right
The correct answer was: True
Your current score is: 4/10


Q.11: No piece of square dry paper can be folded in half more than 7 times. (True/False): True
That's wrong.
The correct answer was: False
Your current score is: 4/11


Q.12: A few ounces of chocolate can to kill a small dog. (True/False): True
You got it right
The correct answer was: True
Your current score is: 5/12


You've completed the quiz
Your final score was: 5/12






Post a Comment

Previous Post Next Post
Best Programming Books

Facebook

AJ Facebook
Checkout Our Facebook Page
AJ Blogs
Checkout Our Instagram Page