如何在Pyton3中调用类wit.eself中的函数

时间:2016-05-02 10:18:07

标签: python function class self

所以我一直在尝试创建一个结构更复杂的计算器。我面临的问题是我试图创建一个调用另一个函数的函数,我知道这似乎是不必要的,但将来还需要它。调用函数时遇到问题。

班级计算器:

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)

2 个答案:

答案 0 :(得分:0)

我希望这可以作为它的出发点:

def div(x, y):
    return x / y

def add(x, y):
    return x + y

def subs(x, y):
    return x - y

def do(operation, x, y):
    return operation(x, y)

print do(add, 4, 2)
print do(subs, 4, 2)
print do(div, 4, 2)

答案 1 :(得分:0)

希望它会对你有所帮助。

代码修改:

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()