Skip to content

Commit ea61fe8

Browse files
Merge pull request geekcomputers#196 from shreydan/master
calculator.py: fixed mod error, new features and code changes
2 parents 9d4802e + 2cc27ef commit ea61fe8

File tree

1 file changed

+51
-42
lines changed

1 file changed

+51
-42
lines changed

calculator.py

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,69 @@
11
"""
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+
66
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)
1822
"""
1923

2024
import math
2125
import sys
2226

2327

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', '%')
2937

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']
3439

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)
3844

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
4458

45-
return k
4659

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)))
5462

55-
print("\n" + str(calc(k)))
5663

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")
5867

5968
if sys.version_info.major >= 3:
6069
while True:

0 commit comments

Comments
 (0)