Create Calculator using Eval in Python – Easiest Way

Hello knowledge seekers, in this blog we will make a simple calculation by method 2.

We have covered another method to create simple calculator, you can check that blog here.

In the first method of calculator creation, we have used user choices and then using conditional branching we have decided which operation needs to be done on the input number.

Here, things will go a little differently, we will ask for user choice and we will definitely use if else condition, but there will be a little bit of a twist in this program.

We have covered this topic and explained everything in this video.

We want you to know about the creating calculator in python, hence we are come up with the best video explanation you have ever watched on the internet, where you not only understand the logic but also code to get the desired output.

You still face difficulties in understanding the logic? That’s perfectly fine, we have something more for you. You can scroll down to learn more about the this topic.

For all the practice Videos and Explanation on Python please click over here. Python Practice Series.

Video Explanation to create a calculator using Eval in python

Code: Create a calculator using Eval in Python

In this program we have used the eval function for the creation of a calculator, so let’s see how this works.

What is eval()?

eval is a built-in- function used in python, eval function parses the expression argument and evaluates it as a python expression.

In simple words, the eval function evaluates the “String” like a python expression and returns the result as an integer.

The syntax of the eval function is as shown below:

eval(expression, [globals[, locals]])

Arguments or Parameters:

The arguments or parameters of eval function are strings, also optionally global and locals can be used as an argument inside eval function, but the globals must be represented as a dictionary and the locals as a mapped object.

Return Value:

The return value would be the result of the evaluated expression. Often the return type would be an integer.

So lets start with the program,

Method 1: Create a calculator using Eval in Python

Source Code and Output

choice = ""
while(1):
    print("+ for addition")
    print("- for subtraction")
    print("* for multiplication")
    print("/ for division")
    print("e for exit")
    
    choice = input("Enter Choice:")
    
    if choice == 'e':
        print("Thanks for using the calculator")
        break
    
    if choice == '+' or choice == '-' or choice == '*' or choice == '/':
        x = input("Enter first number")
        y = input("Enter second number")
        exp = x + choice + y
        sln = eval(exp)
        print(sln)
    else:
        print("Invalid Choice")

Output:

+ for addition
- for subtraction
* for multiplication
/ for division
e for exit
Enter Choice:*
Enter first number5
Enter second number10
50

Code Explanation Method 1: Create a calculator using Eval in Python

At the very beginning of the code, we have declared a variable choice like we have in the last method, but if you see we haven’t assigned any value to it, you will get to know this as we move ahead in the program. You can even declare choice with value None.

Here we have initiated while loop and it will run till true or 1, which is nothing but the infinite loop, The loop will run endlessly.

Inside while loop we have a print statement, which print enter + for addition, – for subtraction, * for multiplication, / for division, and e for the exit.

So here we are asking the user for a specific special character, not an integer like we did in the last method, hence we have declare blank or empty variable choice.

In the next line, we are accepting user input into the choice variable.

In the next line, we are checking whether the user has entered e? If yes we will break the loop with the message “Thanks for using calculator”

To avoid processing we are checking that in the beginning only.

Next we have another if condition where we are checking all the possible choices with the help of or condition.

And if user have entered correct choice then code will go inside if and ask 2 numbers from user.

In the next step we will do out operation .

We will write x + choice +y and store that into a variable called exp.

But wait, this wont work as we are expecting, If x =5 and y =10 and choice is + then instead of 15 this will give us 5 + 10.

Because we have take input choice as string, we have to make it to calculate this expression.

Here we can use eval function of python, we will write sln = eval(exp) and print the sln.

And in the else section we will write wrong choice.

Let’s execute out code.

Enter out choice as * 

Now code will check for condition and it will ask for two input numbers then enter number as 5 and 10 .

See we have correct out as 50.

I hope now you know how to create a calculator using eval function.

Facebook Comments
(Visited 1 times, 1 visits today)

Leave A Comment

Your email address will not be published. Required fields are marked *