Making a Simple Calculator using Dictionaries with Functions in Python

Ayush Dixit
3 min readAug 9, 2021

Recently, i took the challenge of 100 Days of Python coding. A fine example of using Dictionaries with functions is demonstrated below by making a simple calculator which can compute basic mathematical functions such as addition subtraction, multiplication and division.

Depending on User’s choice one can proceed with the earlier calculation or begin with a fresh one. Exactly where python dictionaries come into play providing us with a tool to call mathematical functions as per user specifications.

Step 1 : Defining the functions

Let us first define the four mathematical functions we intend to perform.

print("Welcome to the simple calculator")def add(n1, n2):
return n1 + n2
def sub(n1, n2):
return n1 - n2
def multiply(n1, n2):
return n1 * n2
def divide(n1, n2):
return n1/n2

Step 2 : Taking user inputs and defining a boolean variable

n1 = int(input("What's the first number? "))
operation = str(input("Pick an operation: \n + \n - \n * \n / \n "))
n2 = int(input("What's the next number?: \n"))
dict = {"+": add, "-": sub, "*": multiply, "/": divide}
function = dict[operation]
result = function(n1,n2)
print(f"{n1} {operation} {n2} : {result}")
calculation_finished = False

n1, operation and n2 take the user input for the first variable, operation to be performed and the second variable. For simplicity, we would be inputting integer values only.

dict = {“+”: add, “-”: sub, “*”: multiply, “/”: divide}

We define a dictionary “dict” which depending on the operation decides the respective function to be evaluated.

function = dict[operation]

“operation” here is passed on as a key to the dictionary (dict) and the corresponding value is stored in function.

Let’s say the user inputs addition operation. The input would look like “+” and the corresponding operation is add which is passed on as a function by the result variable and operated upon.

Defining a boolean variable

calculation_finished = False

Here, calculation_finished takes care of the infinite while loop depending on the user input if he wishes to continue calculation.

Step 3: Creating a while loop for continued calculations by the calulator

while not calculation_finished : 
n1 = result
wish_continue = str(input(f"Type 'y' to continue calculating with {result}, or type 'n' to start a new calculation, type 'exit' to exit: "))
if wish_continue == "y":
operation = str(input("Pick an operation: \n + \n - \n * \n / \n"))
function = dict[operation]
n2 = int(input("What's the next number?"))
result2 = function(n1,n2)
print(f"{n1} {operation} {n2} : {result2}")
elif wish_continue == 'n':
print(logo)
n1 = int(input("What's the first number?"))
operation = str(input("Pick an operation: \n + \n - \n * \n / \n"))
n2 = int(input("What's the next number?"))
result = dict[operation]
print(f"{n1} {operation} {n2} : {result}")
elif wish_continue == 'exit':
calculation_finished = True
else:
print("try again")

“wish_continue” variable is used to input user’s choice to continue calculation or exit the calculator program.

wish_continue = str(input(f"Type 'y' to continue calculating with {result}, or type 'n' to start a new calculation, type 'exit' to exit: "))

The exiting logic is managed by the elif statement

elif wish_continue == 'exit':
calculation_finished = True

Which changes the boolean variable’s value to “True” and hence the while loop condition is no longer held true, thereby exiting the loop.

You can find the complete code here.

Did you like my efforts? If Yes, please follow me to get my latest posts and updates or better still, buy me a coffee!☕

--

--

Ayush Dixit

Hi, I’m a postgraduate from IIT-Indore(M.Tech). Specialization in Comm. Signal Processing and Machine Learning/AI. Presently working as an Engineer in Qualcomm.