Building a calculator in python

Learning OOP

Technology: Python
Skill: Programming, OOP

Calculator code in python

Why it was developed?

The calculator code was developed with the purpose of reinforcing basic programming concepts such as variables, conditional structures, functions, and user interaction. This type of project helps to understand the fundamental logic of how a system receives data, processes it, and produces an output. Additionally, it serves as a solid foundation for developing more complex applications in engineering and automation.

Future improvements of the system.

In the future, the system can be significantly improved by implementing new features. For example, a graphical user interface could be added to provide a more user-friendly interaction. It is also possible to include more robust error handling using structures like try-except, as well as expanding the available mathematical operations (powers, roots, trigonometric functions, etc.).

Calculator code

# calculator.py

def calculator():
    print("=== CALCULATOR ===\n")

num1 = float(input("first number: "))
    num2 = float(input("second number: "))

# show options
    print("\nOperations")
    print("+ add")
    print("- subtraction")
    print("* multiply")
    print("/ divide")

# choose one option
    operation = input("\nWhat operation do you want? ")

if operation == "+":
        result = num1 + num2
        print(f"\n{num1} + {num2} = {result}")
    elif operation == "-":
        result = num1 - num2
        print(f"\n{num1} - {num2} = {result}")
    elif operation == "*":
        result = num1 * num2
        print(f"\n{num1} * {num2} = {result}")
    elif operation == "/":
        if num2 != 0:
            result = num1 / num2
            print(f"\n{num1} / {num2} = {result}")
        else:
            print("\n¡Error! No se puede dividir entre 0")
    else:
        print("\nInvalid operation")

while True:
    calculator()
    again = input("\nDo you want to continue? (y/n): ")

if again.lower() != "y":
        print("Goodbye 👋")
        break

General operation of the program

The program follows a logical sequence:

  • Display a welcome message
  • Request two numeric values
  • Shows available operations
  • Receives the selected operation
  • Evaluates the option using conditions
  • Performs the calculator
  • Displays the result

Elements used in the code

Function

def calculator():

Defines the main function of the program.

User input

num1 = float(input("First number: "))
num2 = float(input("Second number: "))

Allows the user to enter numeric numbers.

Menu

print("+ Add")
print("- Subtract")
print("* Multiply")
print("/ Divide")

Displays available operations.

Operation selection

operation = input("Choose an operation: ")

Conditional structures

if operation == "+":
    result = num1 + num2

Executes different actions depending on the user choice.

Error validation

if num2 != 0:

Prevents division by zero.

System logic

The program works as a simple processing system:
Input → Process → Output

Programming type
  • Structured programming
  • Use of functions
  • Conditional logic
  • User interaction
Advantages
  • Easy to understand
  • Modular
  • Scalable
  • Good for learning
Limitations
  • Console-based only
  • No full input validation
  • No graphical interface
  • No operation history
Future improvements
  • Add try-except validation
  • Create a graphical interface
  • Add advanced operations
  • Integrate with external systems
Technical conclusion

This calculator program is a basic but functional implementation of a data processing system. It demonstrates the use of programming fundamentals such as conditions, functions, and user input.