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