Building Class Hierarchies with Inheritance in Python

1.25 hours
  • 3 Learning Objectives

About this Hands-on Lab

Inheritance commonly gets a bad reputation and is fairly easy to misuse, but when building a system that requires multiple variations of the same general concept, then it is the right tool for the job. Knowing when and how to use inheritance is important for being the most productive object-oriented programmer that you can be. In this hands-on lab, you’ll be building the class hierarchy for question types used in a quiz system. By completing this lab, you’ll have demonstrated that you know how to factor class logic across a tree of classes to minimize repeated code and leverage how inheritance allows you to share logic.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Implement the Base Question Class

The base Question class is the class for multiple-choice, single answer questions. For this class to meet the business requirements, it will need to have the proper initialization, a select method, a grade method, and a customized __str__ method. The string output should work by printing the following:


Create the TrueFalseQuestion Class by Subclassing Question

When looking at the TrueFalseQuestion class, we want to simplify the initialization since the options are obvious, and we’d also like to customize the printed representation so that it starts with True/False:, unless the question text provided already begins with the words "True" or "False" (ignoring case). Here’s what the output would look like:


1. [True or False (randomized order)]
2. [Opposite of option 1.]
Create the MultipleSelectQuestion Class by Subclassing Question

The biggest difference between a multiple-choice, multiple correct answer question type and the standard multiple choice question type is that the correct_choice and selected_answer attributes need to work with a list of strings instead of a single string. Changing how those two things operate will potentially require adjusting how select and grade work.

Additionally, the number of correct answers should be included in the printed output for a MultipleSelectQuestion. Here’s an example with three correct answers:

[QUESTION_TEXT] (select 3)


Additional Resources

Your company wants to add a quiz system to help assess whether employees are learning anything from internal webinars. The product owner has requested the ability to create 3 types of questions:

  • Multiple Choice, Single Answer - Presents x number of options with only one correct answer.
  • Multiple Choice, Multiple Answer - Presents x number of options with n correct answers.
  • True/False - Presents the options of True and False for the question.

A question has a few attributes:

  • question_text - The text of the question.
  • choices - The options that should be displayed to the user as a list.
  • corect_choice - The option that is the correct answer.
  • selected_answer - The currently selected option as a string

Additionally, every question object in the system needs to be able to do the following:

  • Present itself to the user - Questions must be printable. The options should be randomized before they are presented to the user. Each of the three question types will have slight variations on how they are printed.
  • Select an answer using the select method - Stores the selected option as selected_answer.
  • Grade itself using the grade method - Returns True/False based on whether the selected answer and the correct choice are equal.

A co-worker has written tests that demonstrate how they would like the code to be used and to help drive the implementation. By running the test within, you'll be able to guide the implementation of the classes and know when your classes meet the requirements.

You can run the unit tests with the following command:

$ python -m unittest
Ran 9 tests in 0.000s


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?