Python Program to Make a Simple Calculator

Python Program to Make a Simple Calculator

Calculators are one of the most basic and useful tools in computing. Building a simple calculator in Python is a great exercise for beginners to understand how arithmetic operations work and how to take user inputs. In this blog post, we will create a simple calculator that can perform basic arithmetic operations such as addition, subtraction, multiplication, and division.

Problem Statement
We need to create a Python program that performs the following operations based on user input:
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)

The user should be prompted to enter two numbers and choose an operation. The program will then perform the operation and display the result.

Approach
The approach is simple:
1. Prompt the user to input two numbers.
2. Ask the user to choose an arithmetic operation.
3. Perform the operation based on the user's choice.
4. Display the result.

Python Code
Copy Code From Here
Here’s the Python code to create a simple calculator:
# Python program to make a simple calculator
# Function to perform addition
def add(x, y):
    return x + y
# Function to perform subtraction
def subtract(x, y):
    return x - y
# Function to perform multiplication
def multiply(x, y):
    return x * y
# Function to perform division
def divide(x, y):
    return x / y
# Display a menu to the user
print("Select operation:")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
# Take input from the user for operation choice
choice = input("Enter choice (1/2/3/4): ")
# Take input from the user for numbers
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Perform the appropriate operation based on user choice
if choice == '1':
    print(f"{num1} + {num2} = {add(num1, num2)}")
elif choice == '2':
    print(f"{num1} - {num2} = {subtract(num1, num2)}")
elif choice == '3':
    print(f"{num1} * {num2} = {multiply(num1, num2)}")
elif choice == '4':
    if num2 != 0:
        print(f"{num1} / {num2} = {divide(num1, num2)}")
    else:
        print("Error! Division by zero is not allowed.")
else:
    print("Invalid input! Please choose a valid operation.")

Explanation of the Code
1. Defining Functions: 
The program defines four separate functions (add(), subtract(), multiply(), and divide()) to handle the four arithmetic operations. Each function takes two arguments (the numbers entered by the user) and returns the result of the respective operation.
2. Menu Display: 
A simple menu is displayed to the user, listing the available operations (Addition, Subtraction, Multiplication, and Division). The user selects the operation by entering a corresponding number (1, 2, 3, or 4).
3. Taking User Input: 
The user is prompted to enter two numbers. These numbers are taken as floating-point values (float()), which allows the calculator to handle both integer and decimal values.
4. Operation Execution: 
The program then checks the user's choice using a series of if-elif-else statements. Depending on the user's input (1, 2, 3, or 4), the respective function is called with the two numbers, and the result is printed on the screen.
5. Handling Division by Zero: 
Special care is taken for division. If the user chooses division and the second number is 0, the program displays an error message to prevent division by zero, which would otherwise cause an error.
6. Error Handling for Invalid Input: 
   - The program displays an error message asking for valid input if the user enters an invalid option (anything other than 1, 2, 3, or 4).

Sample Output
Select operation:
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter choice (1/2/3/4): 1
Enter the first number: 12.5
Enter the second number: 7.3
12.5 + 7.3 = 19.8

In the case of division by zero:
Enter choice (1/2/3/4): 4
Enter the first number: 8
Enter the second number: 0
Error! Division by zero is not allowed.

This is the screenshot of the Jupyter Notebook of the Above Program

Conclusion
This simple calculator program in Python introduces basic programming concepts like functions, conditional statements, and user input handling. By dividing the program into functions for each operation, we make the code modular and easy to understand.
This program can be expanded by adding more complex operations such as modulus, exponentiation, and more. As a beginner, understanding the basics of how calculators work in code will improve your problem-solving and logic-building skills.

Comments