This is a tutorial on making an 8 Ball using Python
To start off this program, you will need to import the “random” library, do this by doing,
import random
Next, you will need to have a variable that will have a random number value from 1-20(because there is 20 8 ball answers), to do this you can use the “randrange” method.
var1 = random.randrange(20)
Next, loop everything forever using:
while True:
After that, use an input to get an user to in put their question, This can be done either by using input or raw_input depending on your python version.
Once you finish that, you should check if the variable which generates the random number(var1) is equal to one of the numbers do this by doing
if var1 == number
Now make this number go up to 20 and put a print statement for each of the numbers.
Your program should look somewhat like this:
import random
var1 = random.randrange(21)
while True:
var1 = random.randrange(21)
raw_input(“Ask your question:”)
if var1 == 1:
print “It is certain”
if var1 == 2:
print “It is decidedly so”
if var1 == 3:
print “Without a doubt”
if var1 == 4:
print “Yes definetly”
(and so on)
The easiest way to do this is to create a list with every answer and pick a random answer.
import random
eightballResponses = [“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.”, “Reply hazy, try again.”,”Ask again later.”,”Better not tell you now.”, “Cannot predict now.”, “Concentrate and ask again.”, “Don’t count on it.”, “My reply is no.”, “My sources say no.”, “Outlook not so good.”, “Very doubtful.”]
print (eightballresponses[random.randrange(0,20)])