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