Calculator
Dear Readers,
Today I would share a super cool application of python programming
by making a calculator:
==>1. Our function:
1
2
3
4
5
6
7
8
9
10
11
12
13 | def operator_func(selection,a,b):
if selection =='add':
return a+b
elif selection =='sub':
return a+b
elif selection =='multiply':
return a+b
elif selection =='divide':
try:
return a/b
except ZeroDivisionError as e :
error = f'error occured :{e}'
return error
|
==>2.Here we select our operator and numbers:
1
2
3 | operator = input('select:\n1.add\n2.subtract\n3.multiply\n4.divide\n')
num1 = int(input('enter number_1\n'))
num2 = int(input('enter number_2\n'))
|
==>3.We get the value attached to the keys of the operator_dict using get method:
1
2 | operator_dict ={'1':'add','2':'sub','3':'multiply','4':'divide'}
selection = operator_dict.get(operator,'select valid operator')
|
==>4.Get our answer:
1 | print(f'your answer is :\n{operator_func(selection,num1,num2)}')
|