Im trying to call the two functions from the numbers class but it returns an error even tho i believe i have coded it correctly.

i have played with the code but cant seem to get it working.

class main():

    num1 = input("please enter a number")

    num2 = input("please enter a second number")

    choice = raw_input("please type \"add\" or \"multiply\" ")

    if (choice =="add"):

        print numbers.addition(num1,num2)

    elif(choice =="multiply"):

        print numbers.multiply(num1, num2)

    else:

        print "That wasnt a valid choice sorry"

class numbers():

    def addition(a, b):

        return a + b

    def multiply(a, b):

        return a*b

any tips or advice would be appreciated.

i now realise my code should be arranged so the numbers class and thus functions should be declared before the class that calls it but i still get an error.

class numbers():

    def addition(a, b):

        return a + b

    def multiply(a, b):

        return a*b

class main():

    num1 = input("please enter a number")

    num2 = input("please enter a second number")

    choice = raw_input("please type \"add\" or \"multiply\" ")

    if (choice =="add"):

        print numbers.addition(num1,num2)

    elif(choice =="multiply"):

        print numbers.multiply(num1, num2)

    else:

        print "That wasnt a valid choice sorry"

Looks like you use class when you should have module. Your numbers class have not any numeric value.

The same function exist in module operator as operator.add and operator.mul. Nothing stops you to use sum function instead of operator.add.

I would not use input in Python 2.

You should(most) read some tutorials/book about using classes.
The way you are doing is not god at all and wrong.
Like basic thing that a class need the self keyword.

Can write an example that makes more sense.

class numbers(object):
    def addition(self,a,b):
        self.a = a
        self.b = b
        return self.a + self.b

    def multiply(self,a,b):
        self.a = a
        self.b = b
        return self.a * self.b

class main(numbers):
    calc = numbers()
    num1 = 5
    num2 = 10
    print calc.addition(num1, num2) #15
    print calc.multiply(num1, num2) #50

I realise i could import a module to do the same functions i wrote.

The point was simply learning an efficient way to call methods between classes.

The way i solved this was:

class Callable:
    
    def __init__(self, function):
        self.__call__ = function

class func:

    def addition(a, b):
        return a + b
    addition = Callable(addition)

    def multiply(a, b):
        return a*b
    multiply = Callable(multiply)

class main:

    num1 = input("please enter a number")

    num2 = input("please enter a second number")

    choice = raw_input("please type \"add\" or \"multiply\" ")

    if (choice =="add"):

        print func.addition(num1,num2)

    elif(choice =="multiply"):

        print func.multiply(num1, num2)

    else:

        print "That wasnt a valid choice sorry"

the "Callable" class acts as a wrapper allowing the functions which were converted to "unbound operators" or something to behave as expected.

that is what the

addition = Callable(addition)

lines are for. Thanks for your input and i will look into what you suggested also.

I realise i could import a module to do the same functions i wrote.

The point was simply learning an efficient way to call methods between classes.

The way i solved this was:

class Callable:

    def __init__(self, function):
        self.__call__ = function

class func:

    def addition(a, b):
        return a + b
    addition = Callable(addition)

    def multiply(a, b):
        return a*b
    multiply = Callable(multiply)

end quote.

A more standard way is to use the staticmethod decorator

class func:
    @staticmethod
    def addition(a, b):
        return a + b

    @staticmethod
    def multiply(a, b):
        return a*b

Ah thank you. :)

I found the wrapper while i was doing a little research into the topic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.