Simple Calculator in Python


Hello Friends! Have you mastered the basics of Python? Now it is time to use these concepts to build something amazing. Firstly we are going with some basic projects here is one called a simple Python calculator. The Calculator does all the basic arithmetic operations like addition, subtraction, etc.

In this project I defined four functions: add, subtract, multiply, and divide. These functions perform the respective arithmetic operations and return the result. For example, the add function takes two numbers as input and returns their sum. Similarly, the other functions also take the numbers as input and return their result.


def add(num1, num2):

    return num1 + num2


def subtract(num1, num2):

    return num1 - num2


def multiply(num1, num2):

    return num1 * num2


def divide(num1, num2):

    if num2 != 0:

        return num1 / num2

    else:

        return "Error: Division by zero"



Then display a welcome message and a list of available operations to the user using print statements. Then the prompt appears where the user had to enter their choice of operation by inputting a number from 1 to 4. This is done using the input function(the input() function is used to take input from the user), and the value is stored in the choice variable. Then again the prompt appears to enter the first and second numbers on which the operation will be performed. The float function is used to convert the user's input into a floating-point number (to handle decimal values), and these values are stored in the variables num1 and num2 respectively.


print("Welcome to the Simple Calculator!")

print("Operations:")

print("1. Addition")

print("2. Subtraction")

print("3. Multiplication")

print("4. Division")


choice = input("Enter your choice (1-4): ")


num1 = float(input("Enter the first number: "))

num2 = float(input("Enter the second number: "))


result = 0



Next, we have an if-elif-else statement to determine the selected operation based on the user's choice. Depending on the value of choice, the corresponding function is called with num1 and num2 as arguments to perform the calculation. The result is stored in the result variable, and the corresponding operator symbol (+, -, *, or /) is stored in the operator variable.


If the user enters an invalid choice (a number other than 1-4), an error message is displayed.


Finally, if a valid choice was made, the calculator displays the expression and the result using a formatted string (f"{num1} {operator} {num2} = {result}"). This line only executes if the user's choice was 1, 2, 3, or 4.


if choice == '1':

    result = add(num1, num2)

    operator = "+"

elif choice == '2':

    result = subtract(num1, num2)

    operator = "-"

elif choice == '3':

    result = multiply(num1, num2)

    operator = "*"

elif choice == '4':

    result = divide(num1, num2)

    operator = "/"

else:

    print("Invalid choice!")


if choice in ['1', '2', '3', '4']:

    print(f"{num1} {operator} {num2} = {result}")



That's it! The program allows the user to perform basic arithmetic operations by selecting the desired operation and providing the necessary numbers.


Here is the Complete code:


def add(num1, num2):

    return num1 + num2


def subtract(num1, num2):

    return num1 - num2


def multiply(num1, num2):

    return num1 * num2


def divide(num1, num2):

    if num2 != 0:

        return num1 / num2

    else:

        return "Error: Division by zero"


print("Welcome to the Simple Calculator!")

print("Operations:")

print("1. Addition")

print("2. Subtraction")

print("3. Multiplication")

print("4. Division")


choice = input("Enter your choice (1-4): ")


num1 = float(input("Enter the first number: "))

num2 = float(input("Enter the second number: "))


result = 0


if choice == '1':

    result = add(num1, num2)

    operator = "+"

elif choice == '2':

    result = subtract(num1, num2)

    operator = "-"

elif choice == '3':

    result = multiply(num1, num2)

    operator = "*"

elif choice == '4':

    result = divide(num1, num2)

    operator = "/"

else:

    print("Invalid choice!")


if choice in ['1', '2', '3', '4']:

    print(f"{num1} {operator} {num2} = {result}")



Thanks for visiting Happy Coding!


Comments

Popular posts from this blog

Python Functions: A Comprehensive Guide with Examples

Python tuples

Python program to Add two numbers