Skip to content

Instantly share code, notes, and snippets.

@raihan-uddin
Created September 28, 2020 09:50
Show Gist options
  • Save raihan-uddin/47d61e161a3c45d4cbe71f0aa91875fe to your computer and use it in GitHub Desktop.
Save raihan-uddin/47d61e161a3c45d4cbe71f0aa91875fe to your computer and use it in GitHub Desktop.
PYTHON CONSOLE CALCULATOR
"""
Functions available are:
--------------------------------------------
+ : addition
- : subtraction
* : multiplication
/ : division
% : percentage
"""
import sys
import math
def calc(term):
# input: term of type str
# output: returns the result of the computed term.
# purpose: This function is the actual calculator and the heart of the application
term = term.replace(' ', '')
term = term.replace('^', '**')
term = term.replace('=', '')
term = term.replace('?', '')
term = term.replace('%', '/100.00')
term = term.lower()
try:
term = eval(term)
except ZeroDivisionError:
print("Can't divide by 0. Please try again.")
except SyntaxError:
print("Syntax Error")
except NameError:
print('Invalid input. Please try again')
except AttributeError:
print('Please check usage method and try again.')
except TypeError:
print("please enter inputs of correct datatype ")
return term
def result(term):
print("\n" + str(calc(term)))
def main():
msg = """Functions available are:
--------------------------------------------
+ : addition
- : subtraction
* : multiplication
/ : division
% : percentage
"""
print(msg)
print("For Example: What is 10+10-12\n\nEnter quit to exit")
while True:
k = input("\nWhat is ")
if k == 'quit' or k == 'exit' or k == 'q':
break
result(k)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment