Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

Using Composition in Python to Separate Concerns

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.

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Advanced
Duration
Clock icon 1h 0m
Published
Clock icon Mar 26, 2021

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    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
    

  2. Challenge

    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.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans