Decided to play around with python for a bit. I saw anish's 8ball in perl and I thought I would do one in python.
I just started python today so if you want to tell me what I could improve with it that would be great.
Need python 2.6 for this to work.
Save this file to filename.py where filename is a name of your choice
then run it with python filename.py
#! /usr/bin/env python
import sys,re,random
while True:
x = raw_input("Please ask a question: ")
n = random.randint(1, 7)
if re.match("^exit$|^close$", x):
print "GoodBye!"
sys.exit()
elif n == 1:
print "The answer lies in your heart"
elif n == 2:
print "I do not know"
elif n == 3:
print "Almost certainly"
elif n == 4:
print "No"
elif n == 5:
print "Why do you need to ask?"
elif n == 6:
print "Go away. I do not wish to answer at this time."
elif n == 7:
print "Time will only tell"
My version.
#!/usr/bin/env python
import random
raw_input("Please ask a question: ")
bases = [
"The answer lies in your heart"
, "I do not know"
, "Almost certainly"
, "No"
, "Instead, ask why you need to ask"
, "Go away. I do not wish to answer at this time"
, "Time will only tell"
]
suffixes = [
"my child"
, "young grasshopper"
]
resp = ', '.join([
random.choice(bases)
, random.choice(suffixes)
])
print resp
(change print resp to print(resp) for Python 3.x)
Example responses
The answer lies in your heart, young grasshopper
Time will only tell, my child
Instead, ask why you need to ask, young grasshopper
import sys,re,random
while True:
x = input("Please ask a question: ")
n = random.randint(1, 7)
if re.match("^exit$|^close$", x):
print("GoodBye!")
sys.exit()
elif n == 1:
print("The answer lies in your heart")
elif n == 2:
print("I do not know")
elif n == 3:
print("Almost certainly")
elif n == 4:
print("No")
elif n == 5:
print("Why do you need to ask?")
elif n == 6:
print("Go away. I do not wish to answer at this time.")
elif n == 7:
print("Time will only tell")
#!/usr/bin/env python
import random
x = raw_input("Please ask a question: ")
n = random.randint(1, 7)
if n == (1):
print("The answer lies in your heart")
elif n == (2):
print("I do not know")
elif n == (3):
print("Almost certainly")
elif n == (4):
print("No")
elif n == (5):
print ("Why do you need to ask?")
elif n == (6):
print("Go away. I do not wish to answer at this time.")
elif n == (7):
print("Time will only tell")
^ cleaner simpler and more standard way of doing it.
I like it. Nice and simple. I can't really think of anyway that could be made simpler.