-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.gyp
More file actions
43 lines (40 loc) · 1.21 KB
/
calc.gyp
File metadata and controls
43 lines (40 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import sys
import math
def calc(term):
term = term.replace(' ', '')
term = term.replace('^', '**')
term = term.replace('=', '')
term = term.replace('?', '')
term = term.replace('%', '/100.00')
term = term.replace('rad', 'radians')
term = term.replace('mod', '%')
term = term.replace('aval', 'abs')
functions = ['sin','cos','tan','pow','sqrt','sinh','cosh','tanh','radians','pi','e']
term = term.lower()
for func in functions:
if func in term:
withmath = 'math.' + func
term = term.replace (func , withmath)
try:
term = eval(term)
except ZeroDivisionError:
print("Can't divide by zero . Please try again :)")
except NameError:
print("Invalid input . Please try again :)")
except AttributeError:
print("Please check usage method :)")
except TypeError:
print("Please enter inputs of correct datatype :)")
except Exception:
print("Wrong operator :)")
return (term)
def result(term):
print("\n" + str(calc(term)))
def main():
while True:
k = input("\nEnter your problem : ")
if k == 'quit':
break
result(k)
if __name__ == '__main__':
main()