1
1
# Program make a simple calculator
2
2
3
- class Calculator :
4
3
4
+ class Calculator :
5
5
def __init__ (self ):
6
- pass
7
-
8
- def add (self ,num1 , num2 ):
9
-
6
+ pass
7
+
8
+ def add (self , num1 , num2 ):
10
9
"""
11
- This function adds two numbers.
12
-
13
- Examples:
14
- >>> add(2, 3)
15
- 5
16
- >>> add(5, 9)
17
- 14
18
- >>> add(-1, 2)
19
- 1
10
+ This function adds two numbers.
11
+
12
+ Examples:
13
+ >>> add(2, 3)
14
+ 5
15
+ >>> add(5, 9)
16
+ 14
17
+ >>> add(-1, 2)
18
+ 1
20
19
"""
21
20
return num1 + num2
22
21
23
- def subtract (self ,num1 , num2 ):
24
-
22
+ def subtract (self , num1 , num2 ):
25
23
"""
26
- This function subtracts two numbers.
27
-
28
- Examples:
29
- >>> subtract(5, 3)
30
- 2
31
- >>> subtract(9, 5)
32
- 4
33
- >>> subtract(4, 9)
34
- -5
24
+ This function subtracts two numbers.
25
+
26
+ Examples:
27
+ >>> subtract(5, 3)
28
+ 2
29
+ >>> subtract(9, 5)
30
+ 4
31
+ >>> subtract(4, 9)
32
+ -5
35
33
"""
36
34
return num1 - num2
37
35
38
- def multiply (self ,num1 , num2 ):
39
-
36
+ def multiply (self , num1 , num2 ):
40
37
"""
41
- This function multiplies two numbers.
42
-
43
- Examples:
44
- >>> multiply(4, 2)
45
- 8
46
- >>> multiply(3, 3)
47
- 9
48
- >>> multiply(9, 9)
49
- 81
38
+ This function multiplies two numbers.
39
+
40
+ Examples:
41
+ >>> multiply(4, 2)
42
+ 8
43
+ >>> multiply(3, 3)
44
+ 9
45
+ >>> multiply(9, 9)
46
+ 81
50
47
"""
51
48
return num1 * num2
52
49
53
- def divide (self ,num1 , num2 ):
54
-
50
+ def divide (self , num1 , num2 ):
51
+ """
52
+ This function divides two numbers.
53
+
54
+ Examples:
55
+ >>> divide(4, 4)
56
+ 1
57
+ >>> divide(6, 3)
58
+ 2
59
+ >>> divide(9, 1)
60
+ 9
55
61
"""
56
- This function divides two numbers.
57
-
58
- Examples:
59
- >>> divide(4, 4)
60
- 1
61
- >>> divide(6, 3)
62
- 2
63
- >>> divide(9, 1)
64
- 9
65
- """
66
62
if num2 == 0 :
67
63
print ("Cannot divide by zero" )
68
64
else :
69
65
return num1 / num2
70
- calculator = Calculator ()
71
66
72
67
68
+ calculator = Calculator ()
69
+
73
70
74
71
print ("1.Add" )
75
72
print ("2.Subtract" )
@@ -81,22 +78,21 @@ def divide(self,num1, num2):
81
78
choice = input ("Enter choice(1/2/3/4): " )
82
79
83
80
# Check if choice is one of the four options
84
- if choice in ('1' , '2' , '3' , '4' ):
81
+ if choice in ("1" , "2" , "3" , "4" ):
85
82
num1 = float (input ("Enter first number: " ))
86
83
num2 = float (input ("Enter second number: " ))
87
84
88
- if choice == '1' :
85
+ if choice == "1" :
89
86
print (calculator .add (num1 , num2 ))
90
87
91
- elif choice == '2' :
88
+ elif choice == "2" :
92
89
print (calculator .subtract (num1 , num2 ))
93
90
94
- elif choice == '3' :
95
- print (calculator .multiply (num1 ,num2 ))
91
+ elif choice == "3" :
92
+ print (calculator .multiply (num1 , num2 ))
96
93
97
- elif choice == '4' :
94
+ elif choice == "4" :
98
95
print (calculator .divide (num1 , num2 ))
99
96
break
100
97
else :
101
98
print ("Invalid Input" )
102
-
0 commit comments