Search code examples
pythonfunctionclassself

How to call a function within a class wit .self in Pyton3


So i've been trying to create a calculator with more complex structure. The problem that am facing is that i'm trying to create a function that calls another function, i know it seems unneccesary but it will be needed in the future. I have a problem calling the function.

class Calculator:

class Variables:
    # Start by defining the variables that you are going to use. I created a subclass because I think is better and
    # easier for the code-reader to understand the code. For the early stages all variables are going to mainly
    # assigned to 0.


    n = 0  # n is the number that is going to be used as the main number before making the math calculation

    n_for_add = 0  # n_for_add is the number that is going to added to "n" in addition

    n_from_add = 0  # n_from_add is the result from the addition
    self = 0


def addition(self):
    try:
        n = int(input("enter number: "))  # user enters the n value
        n_for_add = int(input("What do you want to add on " + str(n) + " ? "))  # user enters the n_for_add value
    except ValueError:  # if the value is not an integer it will raise an error
        print("you must enter an integer!")  # and print you this. This will automatically kill the program
    self.n  = n
    self.n_for_add = n_for_add
    n_from_add = n + n_for_add  # this is actually the main calculation adding n and n_for_add
    self.n_from_add = n_from_add
    print(str(n) + " plus " + str(n_for_add) + " equals to " + str(n_from_add))  # this will print a nice output



def subtraction():
    try:
        nu = int(input("enter number: "))
        nu_for_sub = int(input("What do you want to take off " + str(nu) + " ? "))
    except ValueError:
        print("you must enter an integer!")
    nu_from_sub = nu - nu_for_sub
    print(str(nu) + " minus " + str(nu_for_sub) + " equals to " + str(nu_from_sub))
# this is the same as addition but it subtracts instead of adding


def division():
    try:
        num = int(input("enter number: "))
        num_for_div = int(input("What do you want to divide " + str(num) + " off? "))
    except ValueError:
        print("you must enter an integer!")
    num_from_div = num / num_for_div
    print(str(num) + " divided by " + str(num_for_div) + " equals to " + str(num_from_div))
# same as others but with division this time


def multiplication():
    try:
        numb = int(input("enter number: "))
        numb_for_multi = int(input("What do you want to multiply " + str(numb) + " on? "))
    except ValueError:
        print("you must enter an integer!")
    numb_from_multi = numb * numb_for_multi
    print(str(numb) + " multiplied by  " + str(numb_for_multi) + " equals to " + str(numb_from_multi))
 # its the same as others but with multiplication function

def choice(self):
    x = self.addition()
    self.x = x
    return x


choice(self)

Solution

  • Hope it will help you.

    Code modified:

    class Variables:
        def addition(self):
            try:
                n = int(input("enter number: "))  # user enters the n value
                n_for_add = int(input("What do you want to add on " + str(n) + " ? "))  # user enters the n_for_add value
                return n + n_for_add
            except ValueError:
                # if the value is not an integer it will raise an error
                pass
    
        def choice(self):
            x = self.addition()
            self.x = x
            return x
    
    objectVariables = Variables()
    print objectVariables.choice()