work in progress... come back later
Index
- Chapter 1: Adjustments to a Simple CLI Magic 8-Ball
- Chapter 2: The Program in its Purest Form
- Chapter 3: Extending the Program
- Chapter 4: Files
Adjustments to a Simple CLI Magic 8-Ball
A while ago I was looking for an 8-ball cli program (as you do) that would mimic the function of a magic 8-ball. However, when looking for such a program, I couldn't find anything, at least not in the AUR or the official repo, though admittably unsurprising due to such a program being pretty useless outside of messing around.
So I decided to take a look on YouTube and found an old video posted 13 years ago by seaner992(link). There were two issues with this program (in relation to my use case).
- The program was written in Python2 (not Python3).
- There were unneeded lines of code.
As I was learning Python3 at the time (and still am!) I decided to rewrite this program in python3.
The Program in its Purest Form
Below is the original program in its purest form (again written by seaner992) minus the formatting (of which I formatted the answer list).
01 #!/usr/bin/env python
02
03 import random
04
05 answers = ["As I see it, yes",
06 "It is certain",
07 "It is decidedly so",
08 "Most likely",
09 "Outlook good",
10 "Signs point to yes",
11 "Without a doubt",
12 "Yes",
13 "Yes definitely",
14 "You may rely on it",
15 "Reply hazy, try again",
16 "Ask again later",
17 "Better not tell you now",
18 "Cannot predict now",
19 "Concentrate and ask again",
20 "Don't count on it",
21 "My reply is no",
22 "My sources say no",
23 "Outlook not so good",
24 "Very doubtful"]
25
26 raw_input("What is your question?\n")
27
28 response = random.choice(answers)
29
30 print response + '\n'
31
32 while (True):
33 raw_input("What is your question?\n")
34 print random.choice(answers) + '\n'
Now onto how I changed the code...
The first thing I did was removing lines 26-31; due to the presence of the while loop this code is completely unnecessary - it is a just a repetition of what happens in our while loop and without it the program functions the same.
Next I converted all the functions from their python2 version to the newer python3 version (specifically changing raw_input to input and the print statement to print('')).
As a side note we only need the choice function from the random module hence the change of "import random" to "from random import choice".
Below is the new code, rewritten with the two improvements.
01 #!/usr/bin/env python 02 from random import choice 03 04 answers = ["As I see it, yes", 05 "It is certain", 06 "It is decidedly so", 07 "Most likely", 08 "Outlook good", 09 "Signs point to yes", 10 "Without a doubt", 11 "Yes", 12 "Yes definitely", 13 "You may rely on it", 14 "Reply hazy, try again", 15 "Ask again later", 16 "Better not tell you now", 17 "Cannot predict now", 18 "Concentrate and ask again", 19 "Don't count on it", 20 "My reply is no", 21 "My sources say no", 22 "Outlook not so good", 23 "Very doubtful"] 24 25 while True: 26 input("What is your question?\n") 27 print(choice(answers) + '\n')
If you are interested in trying this version of the program out you can skip down to the files section and download the file called 8ball_basic.
You can run it if you are using a unix/linux based OS with either python3 8ball_basic.py or you can use chmod +x && ./8ball_basic