Public » Miscellaneous » Twenty
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

remove Question.ask(), make twenty.py look more like twivy.py

Changeset 57b65a0e1d55

Parent 0d8f9ceaaf2c

by Profile picture of Benjamin PollackBenjamin Pollack

Changes to 2 files · Browse files at 57b65a0e1d55 Showing diff from parent 0d8f9ceaaf2c Diff from another changeset...

Change 1 of 2 Show Entire File brain.py Stacked
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 
1
2
3
 
 
 
 
 
 
 
 
 
 
 
 
 
4
5
6
 
12
13
14
 
 
 
 
 
 
 
 
 
 
 
 
 
15
16
17
@@ -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  
Change 1 of 1 Show Entire File twenty.py Stacked
 
20
21
22
23
 
24
25
 
 
 
 
 
 
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
49
50
 
20
21
22
 
23
24
 
25
26
27
28
29
30
31
32
33
34
35
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@@ -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()