Python Program to Make a Simple Calculator

1. Introduction

A simple calculator is one of the classic projects for beginners in programming. It helps understand basic programming concepts such as functions, loops, and conditional statements. Creating a calculator in Python demonstrates how to perform arithmetic operations based on user input, showcasing the simplicity and versatility of the language. This blog post will guide you through writing a Python program that functions as a simple calculator, capable of performing addition, subtraction, multiplication, and division.

2. Program Steps

1. Display options for the user to choose the desired arithmetic operation.

2. Prompt the user to enter two numbers on which the operation will be performed.

3. Based on the user's choice, perform the selected operation using the input numbers.

4. Display the result of the operation.

5. Provide an option to perform another calculation or exit the program.

3. Code Program

# Function to add two numbers def add(x, y): return x + y # Function to subtract two numbers def subtract(x, y): return x - y # Function to multiply two numbers def multiply(x, y): return x * y # Function to divide two numbers def divide(x, y): return x / y while True: # Step 1: Display operation options print("Options:") print("Enter 'add' to add two numbers") print("Enter 'subtract' to subtract two numbers") print("Enter 'multiply' to multiply two numbers") print("Enter 'divide' to divide two numbers") print("Enter 'exit' to end the program") # Take input from the user choice = input("Enter choice: ") # Step 5: Check if the user wants to exit if choice == 'exit': break # Step 2: Prompt the user for numbers num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) # Step 3: Perform the selected operation if choice == 'add': print("The result is", add(num1, num2)) elif choice == 'subtract': print("The result is", subtract(num1, num2)) elif choice == 'multiply': print("The result is", multiply(num1, num2)) elif choice == 'divide': if num2 != 0: print("The result is", divide(num1, num2)) else: print("Error! Division by zero.") else: print("Invalid Input") 

Output:

Options: Enter 'add' to add two numbers Enter 'subtract' to subtract two numbers Enter 'multiply' to multiply two numbers Enter 'divide' to divide two numbers Enter 'exit' to end the program Enter choice: add Enter first number: 5 Enter second number: 3 The result is 8 

Explanation:

1. The program defines four functions, each implementing an arithmetic operation: addition, subtraction, multiplication, and division. These functions take two arguments and return the result of the operation.

2. It then enters a loop that continuously displays the operation options to the user, takes the user's choice of operation, and prompts the user for two numbers.

3. Based on the user's choice, the corresponding function is called with the input numbers as arguments, and the result is displayed.

4. If the user enters 'divide', the program checks for division by zero to avoid errors.

5. The loop continues until the user chooses to exit by typing 'exit'. This interactive loop allows the user to perform multiple calculations without restarting the program.

6. This simple calculator showcases basic Python syntax and programming concepts, making it a great starter project for beginners.


Comments