1
\$\begingroup\$

This code is for a magic 8 ball in Python. What do you think?

import random
import time
choices=[
"Definitely",
"Yes",
"Probably",
"Mabye",
"Probably Not",
"No",
"Definitely Not",
"I don't know",
"Ask Later",
"I'm too tired"
]
while True:
    input("Ask the mighty 8-Ball a question\nOo")
    for i in range(0,3):
        print("Shaking...")
        time.sleep(1)
    print(random.choice(choices))
\$\endgroup\$
7
\$\begingroup\$

Let's go instruction by instruction.

Imports

They are fine. However, with such a few features used from each module, you could explicitly state them so the reader know what to expect:

from random import choice
from time import sleep

Constants

A little problem on naming here. choices is not meaningful in the context as there is nothing to choose from (for a user perspective). Answers or possibilities seems a better fit to me. You might want to use uppercase variable name as it denotes a constant value.

And, since you do not plan on modifying its value during execution, you could enforce that using a tuple instead of a list:

ANSWERS = (
    "Definitely",
    "Yes",
    "Probably",
    "Mabye",
    "Probably Not",
    "No",
    "Definitely Not",
    "I don't know",
    "Ask Later",
    "I'm too tired",
)

Also note the spacing around = and the indentation to improve readability.

Loops

Infinite loops are great. They are so great, no one ever stopped using one.

Joking aside, the only way for you users to exit your program is by using ctrl-c. You may want to provide a cleaner way; for instance if they input an empty line:

while input(...):
    #do stuff

Inputs

The 'Oo' part on the second line of the input feels weird. Try to come up with a more conventionnal prompt delimiter.

Also remember that the text entered by the user will be right after the printed string: add a space for ease of use.

input("Ask the mighty 8-Ball a question\n> ")

Iterations

range(0, x) is better written as range(x). It has the exact same meaning but reads better.

By convention, variables of a for loop that are unused in their body are named _:

for _ in range(3):

Prints

You could have a fancier output and use that i variable if your output uses variable number of dot:

print("Shaking{}".format("."*i))

Sleeps

One full second between shakings, meaning 3 seconds before an answer seems pretty long for this kind of program. I would reduce that time a bit to 0.6 or 0.7 seconds:

sleep(.7) # remember the imports

Lastly

Nothing more to say for the last line. But don't forget to adapt it to the previous changes:

print(choice(ANSWERS))
\$\endgroup\$
  • \$\begingroup\$ I think that ANSWERS is more appropriate as a list than as a tuple, conceptually. It is more akin to rows in a one-column database table than to one row with many columns. \$\endgroup\$ – 200_success Jan 29 '16 at 4:00
2
\$\begingroup\$

Since i isn't being used, consider replacing it with _ to indicate that it's not being used.

\$\endgroup\$

protected by Vogel612 Nov 16 '18 at 9:43

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?