Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculator Using Object Oriented Programming
#1
Code:
while True:
    class math():
        def math_add(self, add):
            self.add = add
        def math_sub(self, sub):
            self.sub = sub
        def math_mul(self, mul):
            self.mul = mul
        def math_div(self, div):
            self.div = div
    a=int(raw_input("First Number: "))
    b=int(raw_input("Second Number: "))
    c=raw_input("Operation: ")
    math = math()
    math.math_add(a+b)
    math.math_sub(a-b)
    math.math_mul(a*b)
    math.math_div(a/b)
    if c=='+': print math.add
    elif c=='-': print math.sub
    elif c =='*': print math.mul
    elif c =='/': print math.div
    else: print "Please use +, -, *, or /"

Edit: A simpler way to this may actually be something like this:

Code:
class math():
    def math_add(self,a,b):
        self.add = a + b    
math = math()
math.math_add(1,2)
print math.add
Reply


Messages In This Thread
Calculator Using Object Oriented Programming - by nevets04 - 11-15-2009, 07:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: attribute of type 'NoneType' is not callable object chris0147 0 973 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 815 02-23-2013, 09:18 AM
Last Post: joseph george
  'X' object has no attribute 'y' band_master 1 1,111 04-23-2012, 09:54 PM
Last Post: band_master
  [Guide] Object Serialization in Python manipulate 3 1,468 08-12-2011, 10:39 AM
Last Post: BreShiE

Forum Jump:


Users browsing this thread: 1 Guest(s)