How To Make a Calculator Program in Python 3?

How To Make a Calculator Program in Python 3?

Have you ever wondered how to build a simple calculator program in Python 3? In this step-by-step guide, we’ll create a basic calculator for addition, subtraction, multiplication, and division. Don’t worry if you’re new to programming; we’ll keep it simple and easy to understand.

We’ll use Python’s math operators, variables, conditional statements, and functions to build an interactive calculator.

Prerequisites

Before we begin, make sure you have Python 3 installed on your system.

Full Code

Let’s start by creating a file named cal.py and adding the complete code for our simple calculator:

# cal.py

while True:
# Prompt Users for Input
    operation = input("Choose an operation (+, -, *, /): ")
    num1 = float(input("Enter the first number: "))
    num2 = float(input("Enter the second number: "))
    # Adding Operators and Defining Functions
    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 "Oops! You can't divide by zero."
        return x / y
    # Handle the Operation
    if operation == "+":
        result = add(num1, num2)
    elif operation == "-":
        result = subtract(num1, num2)
    elif operation == "*":
        result = multiply(num1, num2)
    elif operation == "/":
        result = divide(num1, num2)
    else:
        result = "Sorry, that's not a valid operation."
    # Adding Conditional Statements for Special Cases
    if operation == "/" and num2 == 0:
        result = "Oops! You can't divide by zero."
    # Display the Result
    print(f"Result: {result}")
    another_calculation = input("Do you want to do another calculation? (yes/no): ")
    if another_calculation.lower() != "yes" and another_calculation.lower() != "y":
        break
print("Thank you for using the Simple Calculator Program. Goodbye!")

Output:

Result

In the output, you can see that users can do basic math operations using symbols like ‘+’, ‘-‘, ‘*’, and ‘/’. The program also detects and deals with incorrect symbols, prevents division by zero errors, and, after each calculation, prompts the user if they want to do more math.

Save $100 in the next
5:00 minutes?

Register Here

Explanation

Now, let’s break down the code step by step:

Step 1. Greeting and Welcome Message: We start by greeting the user and providing a welcome message to make the user’s experience more friendly.

Step 2. Adding Mathematical Operators: We define variables for four mathematical operators: addition, subtraction, multiplication, and division.

Step 3. Defining Functions for Operations: We define functions for each operation. These functions take two arguments (x and y) and return the result of the operation.

Step 4. Prompting for Operation: We prompt the user to choose an operation by entering one of the mathematical operators.

Step 5. Prompting for Numbers: We prompt the user to enter the first and second numbers for the calculation.

Step 6. Performing the Operation: Based on the user’s choice of operator, we call the corresponding function to perform the calculation.

Step 7. Displaying the Result: We display the calculation result to the user.

Step 8. Asking for Another Calculation: We ask the user if they want to perform another calculation. If they respond with “yes” or “y” the program can continue with additional calculations, it will give a thanks message.

Conclusion

You’ve successfully created a simple calculator program in Python 3. This program can perform basic mathematical operations and is ready for further enhancements. You can customize, add functionality, or integrate it into other projects.

To run the program, save it as cal.py and execute it using Python 3:

python3 cal.py

Save $100 in the next
5:00 minutes?

Register Here