- Lab
- A Cloud Guru
Building Class Hierarchies with Inheritance in Python
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.
Path Info
Table of Contents
-
Challenge
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, aselect
method, agrade
method, and a customized__str__
method. The string output should work by printing the following:[QUESTION_TEXT] 1. [OPTION] 2. [OPTION] 3. [OPTION] 4. [OPTION]
-
Challenge
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 withTrue/False:
, unless the question text provided already begins with the words "True" or "False" (ignoring case). Here's what the output would look like:True/False: [QUESTION_TEXT] 1. [True or False (randomized order)] 2. [Opposite of option 1.]
-
Challenge
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
andselected_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 howselect
andgrade
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) 1. [OPTION] 2. [OPTION] 3. [OPTION] 4. [OPTION] 5. [OPTION]
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.