Using Composition in Python to Separate Concerns

1 hour
  • 2 Learning Objectives

About this Hands-on Lab

Composition is an object-oriented programming technique that allows us to create flexible programs by separating the responsibilities across multiple classes that know how to interact with one another. This type of flexibility makes it easier to maintain and develop applications over long periods. In this hands-on lab, you’ll be building a quiz system that utilizes some pre-written question classes and also requires you to create a new type of question without it changing how the overall quiz functions. Completing this lab will demonstrate that you know how to utilize composition when architecting a non-trivial program.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Implement the Quiz Class with Support for Pre-Built Question Types

Create a file named quiz.py that contains the implementation for the Quiz class.

The class must have the following attributes:

  • questions : A list of questions to display to the user.
  • passing_percent: The percentage of questions that must be correct to pass the quiz.
  • actual_score: The number of questions that were answered correctly.

The Quiz class is responsible for doing the following:

  • Randomly displaying questions one at a time to the user by using the start method. While a question is presented, the application should wait for user input and set the resulting message as the answer for the question before moving to the next question.
  • Calculating the number of total correct answers using the score method. This should set the actual_score attribute and return the value.
  • Returning whether or not the quiz was passed using the grade method. The return value should be a tuple with the type of (float, bool), the actual percentage of questions that were answered correctly, and the pass/fail status.

Run the automated tests for the Quiz class using the following command:

$ python -m unittest test_quiz.TestQuiz

....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK

Implement the EssayQuestion Class

The EssayQuestion class needs to implement the same methods as the Question, TrueFalseQuestion, and MultipleSelectionQuestion classes, but it will be simpler and shouldn’t be a subclass of Question.

Create a file named essay_question.py that contains the implementation for the EssayQuestion class.

The class must have the following attributes:

  • question_text : The question itself.
  • answer : The user’s response.
  • answer_is_sufficient : Whether the answer correctly answers the question.

The class is responsible for doing the following:

  • Printing to the screen: When cast to a str, the question should return the question_text.
  • Setting the answer: The select method should take an argument, ensure that it is a string, and set it as the value for answer.
  • Prompt for manual grading : The grade method should print the question_text and the provided answer while prompting the grader with the question "Is this answer sufficient? [Yn]: " (or something similar). The response should be read and then answer_is_sufficient should be set to the boolean value before also returning that boolean value from the function.

There is a single test in the test_quiz.py file that utilizes this class, so once all of the tests are passing, you can feel confident that you’ve implemented the class correctly.

Additional Resources

Your company is building a quiz system to help assess whether employees are learning anything from internal webinars. Some classes have already been created to model various types of multiple-choice questions, and now your team is ready to start working on the grading of an entire quiz. They've asked you to implement the Quiz class. To make this class as flexible as possible, they've asked you to utilize composition so that Quiz itself doesn't need to be involved in scoring individual questions.

The class must have the following attributes:

  • questions - A list of questions to display to the user.
  • passing_percent - The percentage of questions that must be correct to pass the quiz.
  • actual_score - The number of questions that were answered correctly.

The Quiz class is responsible for doing the following:

  • Displaying questions (randomly), one at a time to the user by using the start method. While a question is presented, the application should wait for user input and set the resulting message as the answer for the question before moving to the next question.
  • Calculating the number of total correct answers using the score method. This should set the actual_score attribute and return the value.
  • Returning whether or not the quiz was passed using the grade method. The return value should be a tuple with the type of (float, bool), the actual percentage of questions that were answered correctly, and the pass/fail status.

You've also been tasked with implementing an EssayQuestion class that represents a question that requires a long-form answer. When the grade method is called, the question should print the question_text to the screen and prompt for a user to manually grade it (giving a y or n as to whether the answer is sufficient).

To help guide your creation of this application, a coworker has written some tests to demonstrate how the classes will ideally be used, and a sample script that can be run to see a quiz in action.

You can run all of the unit tests with the following command:

$ python -m unittest
............
----------------------------------------------------------------------
Ran 12 tests in 0.004s

OK

You can run just the quiz unit tests by running this command:

$ python -m unittest test_quiz.TestQuiz

....
----------------------------------------------------------------------
Ran 4 tests in 0.000s

OK

Once the Quiz and EssayQuestion classes have been implemented, you can run the example_quiz.py file to manually test how the quiz system works.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?