|
1 | 1 | """ |
2 | | -Written by: Shreyas Daniel - github.com/shreydan |
3 | | -Description: Uses Pythons eval() function |
4 | | - as a way to implement calculator |
5 | | -
|
| 2 | +Written by : Shreyas Daniel - github.com/shreydan |
| 3 | +Description : Uses Pythons eval() function |
| 4 | + as a way to implement calculator. |
| 5 | + |
6 | 6 | Functions available: |
7 | | -
|
8 | | -+ : addition |
9 | | -- : subtraction |
10 | | -* : multiplication |
11 | | -/ : division |
12 | | -% : percentage |
13 | | -sine: sin(rad) |
14 | | -cosine: cos(rad) |
15 | | -tangent: tan(rad) |
16 | | -square root: sqrt(n) |
17 | | -pi: 3.141...... |
| 7 | +-------------------------------------------- |
| 8 | + + : addition |
| 9 | + - : subtraction |
| 10 | + * : multiplication |
| 11 | + / : division |
| 12 | + % : percentage |
| 13 | + e : 2.718281... |
| 14 | + pi : 3.141592... |
| 15 | + sine : sin(rad) |
| 16 | + cosine : cos(rad) |
| 17 | + tangent : tan(rad) |
| 18 | + remainder : XmodY |
| 19 | + square root : sqrt(n) |
| 20 | + round to nearest integer : round(n) |
| 21 | +convert degrees to radians : rad(deg) |
18 | 22 | """ |
19 | 23 |
|
20 | 24 | import math |
21 | 25 | import sys |
22 | 26 |
|
23 | 27 |
|
24 | | -def main(): |
25 | | - |
26 | | - def calc(k): |
27 | | - |
28 | | - functions = ['sin', 'cos', 'tan', 'sqrt', 'pi','mod'] |
| 28 | +def calc(k): |
| 29 | + |
| 30 | + k = k.replace(' ', '') |
| 31 | + k = k.replace('^', '**') |
| 32 | + k = k.replace('=', '') |
| 33 | + k = k.replace('?', '') |
| 34 | + k = k.replace('%', '/100') |
| 35 | + k = k.replace('rad', 'radians') |
| 36 | + k = k.replace('mod', '%') |
29 | 37 |
|
30 | | - for i in functions: |
31 | | - if i in k.lower(): |
32 | | - withmath = 'math.' + i |
33 | | - k = k.replace(i, withmath) |
| 38 | + functions = ['sin', 'cos', 'tan', 'sqrt', 'pi', 'radians', 'e'] |
34 | 39 |
|
35 | | - try: |
36 | | - k = eval(k) |
37 | | - except ZeroDivisionError: |
| 40 | + for i in functions: |
| 41 | + if i in k.lower(): |
| 42 | + withmath = 'math.' + i |
| 43 | + k = k.replace(i, withmath) |
38 | 44 |
|
39 | | - print("Can't divide by 0") |
40 | | - exit() |
41 | | - except NameError: |
42 | | - print('Invalid input') |
43 | | - exit() |
| 45 | + try: |
| 46 | + k = eval(k) |
| 47 | + except ZeroDivisionError: |
| 48 | + print("Can't divide by 0") |
| 49 | + exit() |
| 50 | + except NameError: |
| 51 | + print('Invalid input') |
| 52 | + exit() |
| 53 | + except AttributeError: |
| 54 | + print('Check usage method') |
| 55 | + exit() |
| 56 | + |
| 57 | + return k |
44 | 58 |
|
45 | | - return k |
46 | 59 |
|
47 | | - def result(k): |
48 | | - k = k.replace(' ', '') |
49 | | - k = k.replace('^', '**') |
50 | | - k = k.replace('=', '') |
51 | | - k = k.replace('?', '') |
52 | | - k = k.replace('%', '/100') |
53 | | - k = k.replace('mod', '%') |
| 60 | +def result(k): |
| 61 | + print("\n" + str(calc(k))) |
54 | 62 |
|
55 | | - print("\n" + str(calc(k))) |
56 | 63 |
|
57 | | - print("\nScientific Calculator\nEg: pi * sin(90) - sqrt(81)\nEnter quit to exit") |
| 64 | +def main(): |
| 65 | + |
| 66 | + print("\nScientific Calculator\nEg: sin(rad(90)) + 50% * (sqrt(16)) + round(1.42^2) - 12mod3\nEnter quit to exit") |
58 | 67 |
|
59 | 68 | if sys.version_info.major >= 3: |
60 | 69 | while True: |
|
0 commit comments