Create Calculator using Eval in Python

Create Calculator using Eval in Python
(Last Updated On: 06/09/2022)

In python we can create two methods to create a calculator; one is simple and the second is in Eval(). In this blog, we will make a simple calculation using Eval().

Also Read: Python Program to Make a Simple Calculator

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 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.

Do 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 this topic.

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

Video Explanation to create a calculator using Eval in python

Program to Create a calculator using Eval in Python

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 the eval function are strings, also optionally global and locals can be used as an argument inside the 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 let’s start with the program,

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: 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 a choice with the value None.

Here we have initiated the 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 prints 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 declared a 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 the 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 our condition. And if the user has entered the correct choice, then the code will go inside if and ask 2 numbers from the user.

In the next step, we will do the operation. We will write x + choice +y and store that into a variable called exp. But wait, this won’t 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 to take the input choice as a string, we have to make it to calculate this expression. Here we can use the eval function of python; we will write sln = eval(exp) and print the sln. And in the else section, we will write the wrong choice. Let’s execute our code.

Enter our choice as * 

Now code will check for conditions, and it will ask for two input numbers then enter numbers 5 and 10. See, we have corrected out as 50.

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