Python 8ball script

By Lord-Harlot on Oct 16, 2009

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"
Sign in to comment.
aboutscript   -  Jan 05, 2013

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
Lord-Harlot  -  Aug 28, 2013

I like it. Nice and simple. I can't really think of anyway that could be made simpler.

Sign in to comment

Lord-Harlot   -  Jan 05, 2012

I would have been better doing that but of course it was my first script so I didn't really know the best way to do it.

 Respond  
QuinnFTW   -  Jan 04, 2012

would have been better the make a list of responses and call on a random index from that list ex: resposes=["yes","no","maybe"] print responses[len(responses)-1]

 Respond  
theholder   -  Apr 27, 2011

IMPORTS

import sys,re,random

END

THIS WILL WORK WITH PYTHON3.2

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")
 Respond  
Lord-Harlot   -  Jul 11, 2010

The trouble with your version PATX is that you have to rerun the script after each use.

 Respond  
AnaBotNowYourGone   -  May 02, 2010

Very good! (I just got Python) This worked fine. PATX may be right or wrong, but IDK. ;)

 Respond  
PATX   -  Oct 30, 2009
#!/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.

 Respond  
Lord-Harlot   -  Oct 26, 2009

Python is so wonderful it doesn't need a closing statement.

 Respond  
jonesy44   -  Oct 22, 2009

I don't do python, so this is a wild stab in the dark really, doesn't the while loop need a closing statement, the languages i use all have them, but like i said, i don't do python...hsss.. 8-)

 Respond  
Ghost-writer   -  Oct 21, 2009

mountaindew > patx, gf.

 Respond  
mountaindew   -  Oct 20, 2009

Not commenting doesn't make something messy, it's a simple set of if statements, pretty self-explanatory...

 Respond  
PATX   -  Oct 20, 2009

! /usr/bin/env python

  1. ^ Weird way to do that.
  2. Indention is wacko! Very strange.
  3. Not commented very good (for those of them who care [not me])
 Respond  
Lord-Harlot   -  Oct 17, 2009

What's so messy about it?

 Respond  
PATX   -  Oct 17, 2009

RePod, i would not say that. This is some really messy code! If this was in a program I think I would cry...

 Respond  
RePod   -  Oct 17, 2009

Hm.. If I had to say.. This would be a script for some sort of program.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.