Simple Calculator Project in Python with Source Code

A calculator is one of the most basic applications that one can build when learning a programming language. In this blog post, we will create a simple calculator using Python that can handle basic arithmetic operations: addition, subtraction, multiplication, and division. 

Requirements 

A good understanding of Python basics, such as functions and taking user input. 

Steps to Create the Calculator 

Setup the Basic Structure: Start by setting up a loop that continues to run until the user decides to exit. 

Take User Input: Prompt the user to input two numbers and the operation they wish to perform. 

Perform the Calculation: Based on the user's choice, perform the arithmetic operation. Display the Result: Show the result to the user. 

Exit Option: Give the user an option to continue with another calculation or exit. 

Source Code

def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

def multiply(x, y):
    return x * y

def divide(x, y):
    if y == 0:
        return "Undefined (division by zero)"
    return x / y

while True:
    print("Options:")
    print("Enter 'add' for addition")
    print("Enter 'subtract' for subtraction")
    print("Enter 'multiply' for multiplication")
    print("Enter 'divide' for division")
    print("Enter 'quit' to end the program")
    user_input = input(": ")

    if user_input == "quit":
        break
    if user_input in ("add", "subtract", "multiply", "divide"):
        x = float(input("Enter first number: "))
        y = float(input("Enter second number: "))

        if user_input == "add":
            print(add(x, y))
        elif user_input == "subtract":
            print(subtract(x, y))
        elif user_input == "multiply":
            print(multiply(x, y))
        elif user_input == "divide":
            print(divide(x, y))
    else:
        print("Invalid Input")

Output:

Options:
Enter 'add' for addition
Enter 'subtract' for subtraction
Enter 'multiply' for multiplication
Enter 'divide' for division
Enter 'quit' to end the program
: add
Enter first number: 10
Enter second number: 20
30.0
Options:
Enter 'add' for addition
Enter 'subtract' for subtraction
Enter 'multiply' for multiplication
Enter 'divide' for division
Enter 'quit' to end the program
: substract
Invalid Input
Options:
Enter 'add' for addition
Enter 'subtract' for subtraction
Enter 'multiply' for multiplication
Enter 'divide' for division
Enter 'quit' to end the program
: subtract
Enter first number: 20
Enter second number: 12
8.0
Options:
Enter 'add' for addition
Enter 'subtract' for subtraction
Enter 'multiply' for multiplication
Enter 'divide' for division
Enter 'quit' to end the program
: multiply
Enter first number: 10
Enter second number: 12
120.0
Options:
Enter 'add' for addition
Enter 'subtract' for subtraction
Enter 'multiply' for multiplication
Enter 'divide' for division
Enter 'quit' to end the program
: divide
Enter first number: 10
Enter second number: 2
5.0
Options:
Enter 'add' for addition
Enter 'subtract' for subtraction
Enter 'multiply' for multiplication
Enter 'divide' for division
Enter 'quit' to end the program
: quit

Conclusion 

Building a simple calculator in Python provides a foundational understanding of handling user input, making decisions using control structures, and performing basic operations. As you progress, you can expand this basic version to include more complex operations, and error handling, or even create a GUI-based calculator using libraries such as Tkinter. The possibilities are endless as you continue your journey with Python!

Comments