Search This Blog

Object Oriented Quiz Project - Python


QUIZ GAME RULES:

  1. In this quiz project, there is a total of 12 questions. And on the basis of the user's typed answer, the score will be awarded to them. Each question is of 1 mark. The answer can be either True or False. 
  2. The program is using an object-oriented approach.
  3. When a user types the wrong or right answer, share these remarks with them: 
  • If wrong: print("That's wrong")
  • If right: print("You got it right!")

When the quiz ends final output should be:

  • You've completed the quiz.
  • Your final score was: final_score/question_number

TODOs

File- main.py :

# TODO 1: the list to store the question data

# TODO 2: import all the dependent classes, methods

# TODO 3: Get the data from the dat.py and allocate the memory to the data(question, answer)

# TODO 4: Append each  allocated data to the list

# TODO 5: Pass the question list to the quiz brain for actions

# TODO 6: Continue asking questions until the end of the question list

# TODO 7: Display the final score and quiz is ended remark


Code- main.py :


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

question_bank = []


for question in question_data:
    q_text=question['text']
    q_answer=question['answer']

    new_question = Question(q_text,q_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"You final score was: {quiz.score}/{quiz.question_no}")


File- data.py :

# TODO 1: Store the dictionary data into a list.

Code- data.py :

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"}


File- question_model.py :

# TODO 1: Design a template for each question to store questions and answers at the right place.

Code- question_model.py :

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

File- quiz_brain.py :

# TODO 1: asking the questions

# TODO 2: checking if the answer was correct

# TODO 3: checking if we're at the end of the quiz

Code- quiz_brain.py :


class QuizBrain:

    def __init__(self,question):
        self.score=0
        self.question_list=question
        self.question_no=0

    def still_has_questions(self):
        return len(self.question_list) > self.question_no

    def check_answer(selfuser_answer,correct_answer):
        if user_answer.lower()==correct_answer.lower():
            print('You got it right!')
            self.score+=1
        elif user_answer=='end' or user_answer=='END':
            exit()
        else:
            print("That's wrong!")
            
            
        print(f'The correct answer was: {correct_answer}')
        print(f"Your current score is {self.score}/{self.question_no}")
        print('\n')


    def next_question(self):
        
        #if you'll not add the .text after current question then it will
        #show the memory allocatons of the object so better use
        #current_location.text to access the text of queston
        current_questionself.question_list[self.question_no]
        self.question_no+=1
        user_answer=input(f"Q.{self.question_no}{current_question.text}  (True/False): ")
        
        correct_answer=current_question.answer

        self.check_answer(user_answer,correct_answer)


No comments:

Post a Comment