Python program that makes a simple calculator:

Created with Sketch.

Python program that makes a simple calculator:

# Function to perform arithmetic operations
def calculator(operator, num1, num2):
    if operator == '+':
        return num1 + num2
    elif operator == '-':
        return num1 - num2
    elif operator == '*':
        return num1 * num2
    elif operator == '/':
        return num1 / num2
    elif operator == '%':
        return num1 % num2
    elif operator == '**':
        return num1 ** num2
    else:
        return "Invalid operator"

# Take input from the user
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
operator = input("Enter operator (+, -, *, /, %, **): ")

result = calculator(operator, num1, num2)
print("Result: ", result)

This program defines a function calculator() that takes an operator and two numbers as arguments. The function uses an if-elif statement to check the operator and perform the appropriate arithmetic operation on the two numbers and return the result. The program then prompts the user to enter two numbers and an operator and stores the values in the variables “num1”, “num2” and “operator”. It then calls the calculator function and pass the operator, num1, num2 as arguments and assigns the returned value to the variable “result” and print the result.

Leave a Reply

Your email address will not be published. Required fields are marked *