In this lab, you will work with a quiz application that asks a sequence of questions of the user. The interface is shown here:
The application displays problems in a text area (on the top) and messages (on the bottom right); the user types answers in a text box (on the bottom left) and presses enter.
Three classes work together to create the application:
Create a new package for this lab called lab13
and copy
the starting code files into this package:
Familiarize yourself with the quiz mechanism by doing the following:
Right now our quiz mechanism can only ask short answer problems, but
this is a bit too limited for our purposes. We'd like to add
fill-in-the-blank problems, true-false problems, and maybe even
multiple choice problems. As we start planning, we realize that we
will be duplicating code if we write each kind of problem from
scratch. Instead, let's start with a Problem
class that
will be the parent class of all the different kinds of problems. This
class will collect all of the attributes and methods that are shared
between all problems.
So what is shared between all problems? Each problem has some text, but asks the question in a different way (e.g. short-answer just added a question mark, but a fill-in-the-blank problem should add both the question mark and an indication to "Fill in the blank."). Further, all problems have answers, but a true-false problem has a boolean answer instead of a string. Considering these properties, we proceed as follows:
Refactor your code to include a Problem
class and a ShortAnswer
class that inherits from Problem
.
The new Problem
class:
self.text
get_text()
The updated ShortAnswer
class:
Problem
Problem
constructor to initialize
the text instance variable (instead of doing the
assignment statement itself): Problem.__init__(self, q)
ask_question
method that replaces
the access of self.text
with a call to the appropriate
accessor in the Problem class and then appends the question mark: self.get_text() + '?'
get_text
method (since the Problem
class is taking care of this for us).
If all has gone well, your controller should run just as it did before, and all of the ShortAnswer tests should still pass.
Python does not require that the question class definitions be placed in separate files, but it is common practice to separate more complex classes into separate files. These question classes are probably simple enough that they can be kept together, but if you choose this option, you should rename the file to “problem” to more accurately indicate what it contains.
We are now ready to add more types of problems, such as true-false problems and fill-in-the-blank problems. Examples of how your application should present these questions are shown below:
![]() |
![]() |
The new problem classes are very similar to a ShortAnswer problem, but they each have differences:
FillInTheBlank
class is very
similar to the ShortAnswer
class (so similar we should
maybe not have a whole separate class, but this is for learning
purposes, right?) with the following modification:
ask_question
method must append the string
'\nFill in the blank.'
to the end of the problem text
instead of just a question mark.
TrueFalse
class is also similar, but with a
few more details to be worked out:
bool
.
You can check that answer
is boolean using: isinstance(answer,
bool)
.
ask_question
method must append the string
'\nIs this statement true or false?'
to the end of the
problem text.
get_answer
method should return a string
(to match the other problems): str(self.answer)
check_answer
method will receive a string,
so we must compare a string version of the correct answer to the
received answer.
Because all problems are using the same method names, the quiz will be able to create a list of Problems, and ask each problem to ask its question, check its answer, and tell us the correct answer.
Do the following (if you haven't already):
FillInTheBlank
and TrueFalse
classes as described above.
Your quiz application should now operate as it did before, with randomly ordered questions of all three subtypes.
Submit the final version of all of your files. We will grade this exercise according to the following criteria:
exercise 13.2
- Add the required
inheritance.
exercise 13.3
- Configure the
polymorphic behavior.
If you’re working on a lab computer, don’t forget to log off of your machine when you are finished!