-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalculator.py
74 lines (57 loc) · 1.39 KB
/
calculator.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
Problem
-------
**Calculator**
A simple calculator to do basic operators.
Make it a scientific calculator for added complexity.
Solution
--------
This program executes basic operation between float
Author
------
dbonadiman
"""
import sys
def operation(n1, n2, op):
"""
Converts a binary string to a real number!
>>> operation(3, 2, '+')
5
>>> operation(3, 2, '-')
1
>>> operation(3, 2, '*')
6
>>> operation(3.0, 2.0, '/')
1.5
>>> operation(3.0, 2.0, '^')
Traceback (most recent call last):
...
ValueError: Wrong operator only + - * / allowed
"""
if op not in "+-*/":
raise ValueError("Wrong operator only + - * / allowed")
dic = {
'+': n1+n2,
'-': n1-n2,
'*': n1*n2,
'/': n1/n2
}
return dic.get(op)
def main():
try:
print("\nThis program converts decimal number to binary"
"and vice-versa.\n")
n1 = float(raw_input("Number 1: "))
n2 = float(raw_input("Number 2: "))
operator = str(raw_input("Operator (+, -, *, /): "))
print("\n{} {} {} = {}".format(n1, operator,
n2, operation(n1, n2, operator)))
return 0
except Exception, e:
print(e)
return 1
if __name__ == "__main__":
import doctest
doctest.testmod()
status = main()
sys.exit(status)