codingstreets
Search
Close this search box.
python-beginner-project-python-calculator

Python Beginner Project: Python Calculator

As technology is increasing daily, everything has come on a finger, and like it is just one step away from getting on! As developers are now performing on new and upcoming projects, everything has become easy to get started with. We can’t count on how many projects have been made possible in the world, and now we’re human surrounded by them.

So, let’s talk about one of those projects we’re surrounded by and have made possible for everyone!

So, today we will talk about one of the beginner projects of Python, i.e., Python Calculator. It sounds weird to talk about a calculator? A calculator is just 2+3=5, isn’t it? Yes! But today, we are not performing operations on numbers; instead, we will learn how a calculator is created and run on the Python Console. Isn’t it fun to get started?

So, what are you waiting for? Let’s get started with Python Calculator, explore the world of operators & numbers, and connect the mind with calculation!

Prerequisite for Python Calculator – 

Python def function

Python Operators

Python while Loop

Python String

Python if… else Statement

Let’s take a look at Python Calculator before getting started!

Let’s think practically! What is the first thing we enter to perform the calculation? It is a number! Rewind the process and think in what structure we enter the input to get the output performed based on numbers.

For example: 

2 + 3 = 5

Means: First number + operator + second number = Result (First number + second number) 

In this same way, we are going to learn how to create a Python Calculator.

Step 1:

First, we used a pre-defined Python function named input to take the number from the user and convert that number from class<string> to class<int> by using the int function.

Note: By default Python input function returns output in class<string>.

In the same way, we do for the operators. We have a choice from several operators to choose the operator. But the user doesn’t choose the operator from the options or write anything else; then, the Python interpreter shows a custom message (error) and asks the user to choose the operator from the given options. This process continues until the user chooses an operator given in the options. And to keep asking the user for the operator continuously, we have used a while loop that works until a condition meets TRUE. Finally, after choosing an operator, we asked for the second number from the user by following the same procedure we followed for asking for the first number.

Code looks like:

				
					    first_number = int(input("Enter the first number: "))
    while True:
        operator = input("Choose one operator: (+, -, *, /, %): ")
        if operator == "+":
            second_number = int(input("Enter the second number: "))
            addition()
            break
        if operator == "-":
            second_number = int(input("Enter the second number: "))
            subtraction()
            break
        if operator == "*":
            second_number = int(input("Enter the second number: "))
            multiplication()
            break
        if operator == "/":
            second_number = int(input("Enter the second number: "))
            division()
            break
        if operator == "%":
            second_number = int(input("Enter the second number: "))
            percentage()
            break
        else:
            print("Invalid operator")

				
			

Step 2:

Also, later on, we used the def function to create an individual function for each number and called them according to operation from inside the if statement. We used the break keyword to stop the answer and not repeat the condition.

Code looks like:

				
					def calculator():
    def addition():
        print(first_number, "+" , second_number, "=", first_number+second_number)
    def subtraction():
        print(first_number, "-" , second_number, "=", first_number-second_number)
    def multiplication():
        print(first_number, "*" , second_number, "=", first_number*second_number)
    def division():
        print(first_number, "/" , second_number, "=", first_number/second_number)
    def percentage():
        print(first_number, "%" , second_number, "=", (first_number*second_number)/100)
calculator()

				
			

Now, we put all the codes of Step 1 and Step 2 inside the def function named calculator() in the following way: Step 2 + Step 1.

Code looks like:

				
					def calculator():
    def addition():
        print(first_number, "+" , second_number, "=", first_number+second_number)
    def subtraction():
        print(first_number, "-" , second_number, "=", first_number-second_number)
    def multiplication():
        print(first_number, "*" , second_number, "=", first_number*second_number)
    def division():
        print(first_number, "/" , second_number, "=", first_number/second_number)
    def percentage():
        print(first_number, "%" , second_number, "=", (first_number*second_number)/100)

    print()     #for inserting a blank line
    first_number = int(input("Enter the first number: "))
    while True:
        operator = input("Choose one operator: (+, -, *, /, %): ")
        if operator == "+":
            second_number = int(input("Enter the second number: "))
            addition()
            break
        if operator == "-":
            second_number = int(input("Enter the second number: "))
            subtraction()
            break
        if operator == "*":
            second_number = int(input("Enter the second number: "))
            multiplication()
            break
        if operator == "/":
            second_number = int(input("Enter the second number: "))
            division()
            break
        if operator == "%":
            second_number = int(input("Enter the second number: "))
            percentage()
            break
        else:
            print("Invalid operator")
calculator()
while True:
    print()     # for inserting a blank line
    ask_user = input("Do you want to calculate again? y/n: ")
    if ask_user == "y":
        print()     # for inserting a blank line
        calculator()
    elif ask_user == "n":
        print("Thanks for using Python Calculator. Have a great day!")
        print()     # for inserting a blank line
        break
    else:
        print("Invalid answer.")        

				
			

In the end, we finally asked the user whether they wanted to calculate again after the one-time calculation or not. For this, we put conditions using if statements and keep asking until a condition is met TRUE by using a while loop.

If the condition is TRUE, we call the calculator() function again to ask the user to perform the calculation, or we greet the user and end the result.

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on