Top

Python 8ball script


Python Code
+ 1 likes
Please Register to submit score.
Bookmark and Share
Average Score  6.5 (of 2 scores)
Date Added  Oct 16, 2009
Last Updated  Oct 21, 2009
Tags  8ball  ball  eight  python 

Introduction

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

Grab the Code

#! /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"
 
 

Comments

  (9)  RSS
RePod
Comments: 14
 
Python Snippet:  Python 8ball script
Posted on Oct 17, 2009 10:01 am
Hm.. If I had to say..
This would be a script for some sort of program.
PATX
Comments: 390
 
Python Snippet:  Python 8ball script
Posted on Oct 17, 2009 3:47 pm
RePod, i would not say that. This is some really messy code! If this was in a program I think I would cry...
Lord-Harlot
Comments: 60
 
Python Snippet:  Python 8ball script
Posted on Oct 17, 2009 5:35 pm
What's so messy about it?
PATX
Comments: 390
 
Python Snippet:  Python 8ball script
Posted on Oct 20, 2009 6:31 pm
#! /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])
mountaindew
Comments: 1,826
 
Python Snippet:  Python 8ball script
Posted on Oct 20, 2009 7:01 pm
Not commenting doesn't make something messy, it's a simple set of if statements, pretty self-explanatory...
Ghost-writer
Comments: 255
 
Python Snippet:  Python 8ball script
Posted on Oct 21, 2009 5:05 am
mountaindew > patx, gf.
jonesy44
Comments: 1,856
 
Python Snippet:  Python 8ball script
Posted on Oct 22, 2009 8:17 pm
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-)
Lord-Harlot
Comments: 60
 
Python Snippet:  Python 8ball script
Posted on Oct 26, 2009 3:34 pm
Python is so wonderful it doesn't need a closing statement.
PATX
Comments: 390
 
Python Snippet:  Python 8ball script
Posted on Oct 30, 2009 8:18 pm
Code:

#!/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.

Commenting Options

Register or Login to Hawkee.com or use your Facebook or Twitter account by clicking the corresponding button below.

  
Bottom