Changeset 0d8f9ceaaf2c…
Parent 59e0bd62d49a…
by Benjamin Pollack
Changes to one file · Browse files at 0d8f9ceaaf2c Showing diff from parent 59e0bd62d49a Diff from another changeset...
@@ -14,7 +14,31 @@ else:
print 'Please type "yes" or "no"'
-class Determiner(object):
+class Question(object):
+ """abstract class for thing that can be asked"""
+
+ def is_possible_answer(self):
+ """return whether I'm a final answer (e.g., a thing, person, etc.)"""
+ raise NotImplementedError
+
+ def question(self):
+ """returns the question that I represent"""
+ raise NotImplementedError
+
+ def ask(self):
+ """asks the question on the command line
+
+ You can use Determiner.question() to grab the question text,
+ and manually call Questioner.advance_yes() or
+ Questioner.advance_no based on the answer, if you want to
+ ask users questions in some other way (see twivy.py)
+ """
+ if ask(self.question()):
+ self.questioner.advance_yes()
+ else:
+ self.questioner.advance_no()
+
+class Determiner(Question):
"""represents a question that helps us narrow down possible answers
Determiners need to know about their questioner, so they can let
@@ -22,6 +46,7 @@ true_question and a false_question property that stores the next
thing to ask if the user says yes/no, respectively.
"""
+
def __init__(self, questioner, question, true_question, false_question):
self.questioner = questioner
self._question = question
@@ -29,27 +54,13 @@ self.false_question = false_question
def question(self):
- """returns the question as a string"""
return self._question
- def ask(self):
- """asks the user on the command line
-
- You can use Determiner.question() to grab the question text,
- and manually call Questioner.advance_yes() or
- Questioner.advance_no(), if you want to ask users questions in
- other ways. This is here just for convenience.
- """
- if ask(self.question()):
- self.questioner.advance_yes()
- else:
- self.questioner.advance_no()
-
def is_possible_answer(self):
False
-class Thing(object):
+class Thing(Question):
"""represents a concrete thing that might be the right answer
For example, valid Things might be "a dog", "a cat", "Apollo 11",
@@ -60,26 +71,8 @@ self.what = what
def question(self):
- """returns the question as a string
-
- This nicely formats things for you in the form of a question
- so you don't have to.
- """
return 'Are you thinking of %s?' % self.what
- def ask(self):
- """asks the user on the command line
-
- You can use Determiner.question() to grab the question text,
- and manually call Questioner.advance_yes() or
- Questioner.advance_no(), if you want to ask users
- questions in other ways. This is here just for convenience.
- """
- if ask(self.question()):
- self.questioner.advance_yes()
- else:
- self.questioner.advance_no()
-
def is_possible_answer(self):
return True
|
Loading...