Modules in python.

Hi All,

I have just made a neat little Module in Python and I thought I would share this too.

This Module tests a number to see if it is an odd number or a even number.

Importing  the Odd Even Module code below.


#Import the Module.
import OddOrEvenModule
#Import the OddEven Class with in the Module.
OddEvenMachine=OddOrEvenModule.OddEven()
#Imports the debug method/funtion of the Module.
OddEvenMachine.OddEvenDebug()
#Imports the main class method/funtion of the Module.
OddEvenMachine.OddEvenEval()

Odd Even Module code below.


#Defines the class.
class OddEven:
    #Defines the class attributes/variables.
    def __init__(self):
        self.typeNum = int(raw_input("Enter a number: "))
    #Defines the main class method/funtion of the Module. Eval is short for evaluator. 
    def OddEvenEval(self):
        #Checks the number given.
        if self.typeNum % 2 == 0:
            #Prints the result of the number given.
            print self.typeNum, "a is even number."
            if self.typeNum == 42:
                 print "42 is also is the meaning of life!"
        else:
            print self.typeNum, "a is odd number."

    #This is just here to check the Module is working form a read level.
    def OddEvenDebug(self):
        print "Debug Works!"        

See if you can spot a hitchhiker’s guide to the galaxy reference in the code too 🙂

This entry was posted in Code, Experiments. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *