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
49
50
51
52
53
54
55
56
57
58
|
#!/usr/bin/env python3
import random
import sys
# Source: https://en.wikipedia.org/wiki/Magic_8-ball#Possible_answers
committal_responses = [
# Affirmative
"It is certain.",
"It is decidedly so.",
"Without a doubt.",
"Yes definitely.",
"You may rely on it.",
"As I see it, yes.",
"Most likely.",
"Outlook good.",
"Yes.",
"Signs point to yes.",
# Negative
"Don't count on it.",
"My reply is no.",
"My sources say no.",
"Outlook not so good.",
"Very doubtful.",
]
non_committal_responses = [
"Reply hazy, try again.",
"Ask again later.",
"Better not tell you now.",
"Cannot predict now.",
"Concentrate and ask again.",
]
def djb2_hash(text):
res = 5381
for c in text:
res = res * 33 + ord(c)
return res
def get_committal_response(question):
return committal_responses[djb2_hash(question) % len(committal_responses)]
def get_response(question):
if random.random() < len(non_committal_responses) / len(committal_responses):
return random.choice(non_committal_responses)
else:
return get_committal_response(question)
if __name__ == "__main__":
try:
while True:
print(get_response(input("> ")))
except (KeyboardInterrupt, EOFError):
sys.exit(0)
|