aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoruntir_l <87096069+untir-l@users.noreply.github.com>2022-03-01 08:09:39 +0000
committeruntir_l <87096069+untir-l@users.noreply.github.com>2022-03-01 08:09:39 +0000
commit64b2658e7d6024b185307957e117d3d501da5f32 (patch)
treecc07ac844680ff0981c5df905056327da113d5b6
parentfccac2034e56f107df209ac76dbb35ecb8e4b285 (diff)
download8hash-64b2658e7d6024b185307957e117d3d501da5f32.tar
8hash-64b2658e7d6024b185307957e117d3d501da5f32.tar.gz
8hash-64b2658e7d6024b185307957e117d3d501da5f32.zip
Format code with black
-rw-r--r--README.md4
-rwxr-xr-xeight_hash.py77
2 files changed, 43 insertions, 38 deletions
diff --git a/README.md b/README.md
index 76bf350..1d19a3c 100644
--- a/README.md
+++ b/README.md
@@ -12,10 +12,12 @@ It is somewhat strange that magic 8-balls, both physical and digital, can return
Of course, if you get an answer not to your liking, you can reword your query to try for a new one.
-## Usage
+## Usage and info
Run `./eight_hash.py` for interactive use, or `import eight_hash` to use as a library. We have to spell out eight because identifiers can't start with a number :(.
+This is a simple script in Python 3. The code style is the defaults of the `black` code formatter.
+
## Copying
The author of this software is Arjun Satarkar.
diff --git a/eight_hash.py b/eight_hash.py
index 7211d5a..e688310 100755
--- a/eight_hash.py
+++ b/eight_hash.py
@@ -1,55 +1,58 @@
#!/usr/bin/env python3
-import sys
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.",
+ # 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.",
+ "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
+ 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)]
+ 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 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)
+ try:
+ while True:
+ print(get_response(input("> ")))
+ except (KeyboardInterrupt, EOFError):
+ sys.exit(0)