Changeset 57b65a0e1d55…
Parent 0d8f9ceaaf2c…
by Benjamin Pollack
Changes to 2 files · Browse files at 57b65a0e1d55 Showing diff from parent 0d8f9ceaaf2c Diff from another changeset...
@@ -1,19 +1,6 @@ import os
import pickle
-def ask(question):
- """ask a question, and return True if the answer yes, False otherwise"""
- while True:
- print question,
- answer = raw_input().strip().lower()
- print ''
- if answer in ('y', 'yes', 'yeah', 'si'):
- return True
- elif answer in ('n', 'no', 'nope'):
- return False
- else:
- print 'Please type "yes" or "no"'
-
class Question(object):
"""abstract class for thing that can be asked"""
@@ -25,19 +12,6 @@ """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
|
@@ -20,31 +20,34 @@ def play():
questioner = load(BRAIN_DB)
questioner.start()
- while True:
+ while not questioner.out_of_ideas() and not questioner.solved():
question = questioner.next_question()
- question.ask()
+ response = ask(question.question())
+ if response == True:
+ questioner.advance_yes()
+ else:
+ questioner.advance_no()
+
if questioner.solved():
print 'I got it!\n'
if ask('Play again? '):
questioner.start()
else:
save(BRAIN_DB, questioner)
- exit()
- else:
- if questioner.out_of_ideas():
- print 'I give up. What were you thinking of?'
- thing = raw_input()
- if questioner.needs_clarification():
- print 'What is a question whose answer is "true" for %s, but "false" for %s?' % (thing, questioner.best_guess())
- question = raw_input()
- questioner.learn_thing_and_question(question, thing)
- else:
- questioner.learn_thing(thing)
- if ask('Play again?'):
- questioner.start()
- else:
- save(BRAIN_DB, questioner)
- exit()
+ elif questioner.out_of_ideas():
+ print 'I give up. What were you thinking of?'
+ thing = raw_input()
+ if questioner.needs_clarification():
+ print 'What is a question whose answer is "true" for %s, but "false" for %s?' % (thing, questioner.best_guess())
+ question = raw_input()
+ questioner.learn_thing_and_question(question, thing)
+ else:
+ questioner.learn_thing(thing)
+
+ if ask('Play again?'):
+ questioner.start()
+ else:
+ save(BRAIN_DB, questioner)
if __name__ == '__main__':
play()
|
Loading...