Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculator Using Object Oriented Programming
#2
That cant be efficent lol
This may help you a bit, I threw it together real quick

Code:
#!/usr/bin/env python

class Calculations( object ):
    def Addition( self, Numbers ):
        return sum( Numbers )
    def Subtract( self, Numbers ):
        Total = Numbers[ 0 ]
        for Index, Num in enumerate( Numbers ):
            try: Total -= Numbers[ Index + 1 ]
            except IndexError: return Total
    def Multiply( self, Numbers ):
        Total = 1.0
        for Num in Numbers: Total *= Num
        return Total
    def Divide( self, Numbers ):
        Total = Numbers[ 0 ]
        for Index, Num in enumerate( Numbers ):
            try: Total /= Numbers[ Index + 1 ]
            except IndexError: return Total
Numbers, Counter, Answer = [], 0, True
while Answer:
    Counter += 1
    try:
        Answer = int( raw_input( "Number %i: " % ( Counter ) ) )
        Numbers.append( Answer )
    except ValueError: Answer = False
if len( Numbers ) < 2: print "Error: You need atleast two numbers..."
else:
    Methods, Method = ["add", "subtract", "divide", "multiply"], False
    while not Method in Methods: Method = raw_input( "Add/Subtract/Divide/Multiply? : ").lower()
    Math = Calculations( )
    if Method == Methods[ 0 ]: print Math.Addition( Numbers )
    if Method == Methods[ 1 ]: print Math.Subtract( Numbers )
    if Method == Methods[ 2 ]: print Math.Divide( Numbers )
    if Method == Methods[ 3 ]: print Math.Multiply( Numbers )
[Image: nv70ad.png]
Terrorcore, unleash, extermination
Hyper real, cold blood, determination
fudge them, I like this sensation
Incredible, I from the annihilation
Reply


Messages In This Thread
RE: Calculator Using Object Oriented Programming - by Fallen - 11-16-2009, 04:12 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: attribute of type 'NoneType' is not callable object chris0147 0 974 09-05-2014, 07:42 PM
Last Post: chris0147
  Programming help nagarjungupta 2 1,038 08-02-2013, 12:28 AM
Last Post: nagarjungupta
  python programming for hamiltonian cycle joseph george 0 816 02-23-2013, 09:18 AM
Last Post: joseph george
  'X' object has no attribute 'y' band_master 1 1,112 04-23-2012, 09:54 PM
Last Post: band_master
  [Guide] Object Serialization in Python manipulate 3 1,474 08-12-2011, 10:39 AM
Last Post: BreShiE

Forum Jump:


Users browsing this thread: 1 Guest(s)