pythonpython-2.7calculator

Basic Python Calc. - Problems with Bound Methods?


I am trying to make a basic addition class, but I am unable to get python to add the two variables together. When I run the code I get <unbound method Calc.add>. How would I resolve this?

num1 = (input("Enter 1st Number: "))
num2 = (input("Enter 2nd Number: "))

class Calc(object):
    def __init__(self, num1, num2):
        self.num1 = num1
        self.num2 = num2
    def add(self):
        return num1 + num2
CC = Calc(num1,num2)

print CC.add

Solution

  • You forgot the parentheses to call the method: print CC.add().