Smart calculator in Python

Problem – This smart calculator works on the text statement also. The user need not provide algebraic expression always. It fetches the word form the command (given by the user) and then formulates the expression.


Input  : Hi calculator plz find the lcm of 4 and 8.  
Output : 8 

Input  : Hi smart plz find the  multiplication of 3 and 9. 
Output : 27 

Input  : Hi smart plz end the program. 
Output : Thanks for enjoy with me.


Code : Python code for smart calculation





# main python proghram
response=['Welcome to smart calculator','My name is MONTY',
          'Thanks for enjoy with me ','Sorry ,this is  beyond my ability']
# fetching tokens from the text command
def extract_from_text(text):
    for t in text.split(' '):
        except ValueError:
    return l
# calculating LCM
def lcm(a,b):
    L=a if a>b else b
    while L<=a*b:
        if L%a==0 and L%b==0:
            return L
# calculating HCF
def hcf(a,b):
    H=a if a<b else b
    while H>=1:
        if a%H==0 and b%H==0:
            return H
# Addition
def add(a,b):
    return a+b
# Subtraction
def sub(a,b):
    return a-b
# Multiplication
def mul(a,b):
    return a*b
# Division
def div(a,b):
    return a/b
# Remainder
def mod(a,b):
    return a%b
# Response to command
# printing - "Thanks for enjoy with me" on exit
def end():
    input('press enter key to exit')
def myname():
def sorry():
# Operations - performed on the basis of text tokens
            'SUB':sub,'SUBTRACT':sub, 'MINUS':sub,
            'PRODUCT':mul, 'MULTIPLY':mul,'MULTIPLICATION':mul,
# commands
while True:
    text=input('enter your queries:  ')
    for word in text.split(' '):
        if word.upper() in operations.keys():
                l = extract_from_text(text)
                r = operations[word.upper()] (l[0],l[1])
                print('something went wrong going plz enter again !!')
        elif word.upper() in commands.keys():



--------------Welcome to smart calculator------------
--------------My name is MONTY--------------------

enter your queries:  tell me the hcf of 4 and 8

enter your queries:  hi plz tell me 7 + 8
Sorry ,this is  beyond my ability

enter your queries:  pls add 7 and 8

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.