Getting Things Together
Since all the components are ready to be integrated together, let's create a main.py file and add the following content there:
from question_model import Question
from quiz_data import question_data
from quiz_brain import QuizBrain
from quiz_ui import QuizInterface
from random import shuffle
import html
question_bank = []
for question in question_data:
choices = []
question_text = html.unescape(question["question"])
correct_answer = html.unescape(question["correct_answer"])
incorrect_answers = question["incorrect_answers"]
for ans in incorrect_answers:
choices.append(html.unescape(ans))
choices.append(correct_answer)
shuffle(choices)
new_question = Question(question_text, correct_answer, choices)
question_bank.append(new_question)
quiz = QuizBrain(question_bank)
quiz_ui = QuizInterface(quiz)
print("You've completed the quiz")
print(f"Your final score was: {quiz.score}/{quiz.question_no}")
Let's import all the classes from the different files we have created. In addition to that, we also need shuffle method from the random module and the html module. We have a list question_bank. We are iterating over the question_data that we receive from quiz_data.py file. If you see the sample response, you will find some text such as 'Dragon'. They need to be unescaped using the html.unescape method. We have a choices list that will contain the correct answer as well as the incorrect answers. The list will be shuffled using the shuffle method from the random module. After shuffling, we are creating question using the Question model from quiz_model.py file and appending it the question_bank list.
Next, we're creating an object quiz of QuizBrain class which requires a list of questions. So, we're passing question_bank to it. After that, we're creating an object quiz_ui of QuizInterface class which requires an object of QuizBrain class, so we have passed the newly created quiz object to it.
Now that everything is ready, we are ready to run the application.
$ python main.py3
