Published
August 19, 2024
Category
Python / Python Project
Crafting a Simple Calculator Application Using python

Build a Simple Calculator application using python

Unlocking Your Morning Potential

Hello Coder, welcome to the Codeezy Projects. In this article, we create a Simple Calculator Application Using Python with Complete Source Code.  Our calculator will allow users to perform addition, subtraction, multiplication, and division operations on two numbers.

 Step 1: Function Definitions We start by defining four functions: add, subtract, multiply, and divide. Each function takes two   parameters representing the numbers to be operated on. For example, the add function returns the sum of the two input   numbers.

 Step 2: Menu Display Next, we display a simple menu to the user, presenting the available operations: add, subtract, multiply, and   divide.

 Step 3: User Input and Operation Selection The user is prompted to enter their choice of operation by inputting a corresponding   number from the menu.

 Step 4: Performing Calculations Based on the user’s choice, the code takes two input numbers from the user and calls the     appropriate function to perform the selected operation. The result of the calculation is then printed to the console.

 Step 5: Handling Invalid Input We handle cases where the user enters an invalid choice by displaying an error message.

 Step 6: Continuing or Exiting the Program After each calculation, the user is asked if they want to perform another calculation. If     they choose to continue, the loop continues, allowing them to perform additional calculations. If not, the program ends.

Source code for Calculator with Python 👇👇

				
					print("Welcome to calculator")
def add(a, b):
    return a + b

def subtract(a, b):
    return a - b
def multiply(a, b):
    return xa * b

def divide(a, b):
    if b == 0:
        return "Cannot divide by 0"
    return a / b

print("Select operation which you perform:")
print("1. Addition ")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")

while True:
    choice = input("Enter choice :(1/2/3/4): ")

    if choice in ('1', '2', '3', '4'):
        num1 = float(input("Enter first num: "))
        num2 = float(input("Enter second num: "))

        if choice == '1':
            print("Result:", add(num1, num2))
        elif choice == '2':
            print("Result:", subtract(num1, num2))
        elif choice == '3':
            print("Result:", multiply(num1, num2))
        elif choice == '4':
            print("Result:", divide(num1, num2))
    else:
        print("Invalid input, Please enter a vaild input!")

    next_calculation = input("Do you want to perform another calculation? (yes/no): ")
    if next_calculation.lower() != 'yes':
        break
				
			

Output 👇👇

Calculator Application Using python


Summary

In just a few simple steps, we’ve created a Simple Calculator Application Using Python that provides basic arithmetic functionality. This project serves as a great starting point for beginners to practice their Python skills and understand the fundamentals of programming logic. I hope you liked this article on Crafting a Simple Calculator Application Using python. Feel free to ask valuable questions in the comments section below. Happy coding!