2016-05-02 1 views
0

Так что я пытался создать калькулятор с более сложной структурой. Проблема, с которой я сталкиваюсь, заключается в том, что я пытаюсь создать функцию, которая вызывает другую функцию, я знаю, что она кажется ненужной, но в будущем она понадобится. У меня проблема с вызовом функции.Как вызвать функцию внутри класса wit .self в Pyton3

класса Калькулятор:

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) 
Смежные вопросы