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

switch twenty.py to Unix line endings, too

Changeset 934ac23a0313

Parent 83feb398eb90

by Profile picture of Benjamin PollackBenjamin Pollack

Changes to one file · Browse files at 934ac23a0313 Showing diff from parent 83feb398eb90 Diff from another changeset...

Change 1 of 1 Show Entire File twenty.py Stacked
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
@@ -1,48 +1,48 @@
-#!/usr/bin/env python - -from brain import load, save - -BRAIN_DB = 'brain.db' - -def ask(question): - while True: - print question, - answer = raw_input().strip().lower() - if answer in ('y', 'yes', 'yeah', 'si'): - return True - elif answer in ('n', 'no', 'nope'): - return False - else: - print 'Please type "yes" or "no"' - -def play(): - questioner = load(BRAIN_DB) - questioner.start() - while True: - question = questioner.next_question() - question.ask() - if questioner.solved(): - print 'I got it!' - 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() - -if __name__ == '__main__': - play() +#!/usr/bin/env python + +from brain import load, save + +BRAIN_DB = 'brain.db' + +def ask(question): + while True: + print question, + answer = raw_input().strip().lower() + if answer in ('y', 'yes', 'yeah', 'si'): + return True + elif answer in ('n', 'no', 'nope'): + return False + else: + print 'Please type "yes" or "no"' + +def play(): + questioner = load(BRAIN_DB) + questioner.start() + while True: + question = questioner.next_question() + question.ask() + if questioner.solved(): + print 'I got it!' + 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() + +if __name__ == '__main__': + play()