2016-05-02 12 views
0

私はもっと複雑な構造の電卓を作成しようとしています。私が直面している問題は、別の関数を呼び出す関数を作成しようとしていることです。不必要なように見えますが、将来必要になるでしょう。私はこの関数を呼び出す際に問題があります。Pyton3でクラスウィジェット.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) 

答えて

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

それはうまく働きました。ありがとう、私は今私のバグを解決する方法だと思う:p – mangafas

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) 
関連する問題