0

I am pretty new to python, so I decided to make a basic calculator application using functions, however, whenever I try to invoke one of my calculation functions, it says it is an Unresolved reference to the function. I have looked around, but I am not sure what is happening.

My code:

class BasicCalculator: # define functions def add(x, y): """This function adds two numbers""" return x + y def subtract(x, y): """This function subtracts two numbers""" return x - y def multiply(x, y): """This function multiplies two numbers""" return x * y def divide(x, y): """This function divides two numbers""" return x / y def power(x, y): """This function does a power of two numbers""" return x ** y if __name__ == '__main__': print("Select an operator") print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Divide") print("5. Power") left = int(input("Enter your left number: ")) choice = int(input("Select your operator(1, 2, 3, 4): ")) right = int(input("Select your right number: ")) if choice == '1': #Error is here, it says "Unresolved reference 'add'" print(add(left, right)) else: print("Invalid input") 

4 Answers 4

1

Your code has 4 issues that you need to fix:

1st: You need to add self as the first argument to all your methods inside BasicCalculator class, self is a reference to the current instance of the class:

class BasicCalculator: # define functions def add(self, x, y): # self must be the first argument """This function adds two numbers""" return x + y # ... 

2nd: You need to instantiate your class:

calculator = BasicCalculator() 

3rd: Call your functions like this:

calculator.add(left, right) 

4th: You need to fix your if statement:

if choice == 1: # not '1', because choice is an integer (choice = int(input("..."))) 

All together: Your code should now be as follows:

class BasicCalculator: # define functions def add(self, x, y): """This function adds two numbers""" return x + y def subtract(self, x, y): """This function subtracts two numbers""" return x - y def multiply(self, x, y): """This function multiplies two numbers""" return x * y def divide(self, x, y): """This function divides two numbers""" return x / y def power(self, x, y): """This function does a power of two numbers""" return x ** y if __name__ == '__main__': print("Select an operator") print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Divide") print("5. Power") left = int(input("Enter your left number: ")) choice = int(input("Select your operator(1, 2, 3, 4): ")) right = int(input("Select your right number: ")) calculator = BasicCalculator() if choice == 1: print(calculator.add(left, right)) else: print("Invalid input") 
Sign up to request clarification or add additional context in comments.

2 Comments

What does "self" do in this context? Also what does "calculator = BasicCalculator()" do?
self is a reference to the current instance of the class. calculator = BasicCalculator() means that we instantiate this class (BasicCalculator), in other words, we create an object of type BasicCalculator
0

Your functions are defined inside a class I believe - although the indentation you wrote here is not entirely consistent. You could try: -

  • either remove the first line "class BasicCalculator:"
  • call your functions in the right namespace: BasicCalculator.add(left,right)

1 Comment

If you choose the former, you will need to rethink your indentation.
0

The function add is defined within the class BasicCalculator. You need to do BasicCalculator.add(). You could also just define add outside of the class.

Basically, just change this line print(add(left, right)) to print(BasicCalculator.add(left, right))

Also, its worth doing a check if the other two variables (left and right) are properly initialized. So change if choice == '1': to if (choice == '1') and (left is not None) and (right is not None):

Comments

0

Make an instance of your calculator

choice = input("Select your operator(1, 2, 3, 4): ") # Not an int calc = BasicCalculator() if choice == '1': # You are comparing strings, not ints print(calc.add(left, right)) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.