11# Program make a simple calculator
22
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+ """
511 This function adds two numbers.
612
713 Examples:
@@ -11,11 +17,12 @@ def add(x, y):
1117 14
1218 >>> add(-1, 2)
1319 1
14- """
15- return x + y
20+ """
21+ return num1 + num2
1622
17- def subtract (x , y ):
18- """
23+ def subtract (self ,num1 , num2 ):
24+
25+ """
1926 This function subtracts two numbers.
2027
2128 Examples:
@@ -25,11 +32,12 @@ def subtract(x, y):
2532 4
2633 >>> subtract(4, 9)
2734 -5
28- """
29- return x - y
35+ """
36+ return num1 - num2
3037
31- def multiply (x , y ):
32- """
38+ def multiply (self ,num1 , num2 ):
39+
40+ """
3341 This function multiplies two numbers.
3442
3543 Examples:
@@ -39,11 +47,12 @@ def multiply(x, y):
3947 9
4048 >>> multiply(9, 9)
4149 81
42- """
43- return x * y
50+ """
51+ return num1 * num2
4452
45- def divide (x , y ):
46- """
53+ def divide (self ,num1 , num2 ):
54+
55+ """
4756 This function divides two numbers.
4857
4958 Examples:
@@ -53,11 +62,15 @@ def divide(x, y):
5362 2
5463 >>> divide(9, 1)
5564 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+
5872
5973
60- print ("Select operation." )
6174print ("1.Add" )
6275print ("2.Subtract" )
6376print ("3.Multiply" )
@@ -73,16 +86,16 @@ def divide(x, y):
7386 num2 = float (input ("Enter second number: " ))
7487
7588 if choice == '1' :
76- print (num1 , "+" , num2 , "=" , add (num1 , num2 ))
89+ print (calculator . add (num1 , num2 ))
7790
7891 elif choice == '2' :
79- print (num1 , "-" , num2 , "=" , subtract (num1 , num2 ))
92+ print (calculator . subtract (num1 , num2 ))
8093
8194 elif choice == '3' :
82- print (num1 , "*" , num2 , "=" , multiply (num1 , num2 ))
95+ print (calculator . multiply (num1 ,num2 ))
8396
8497 elif choice == '4' :
85- print (num1 , "/" , num2 , "=" , divide (num1 , num2 ))
98+ print (calculator . divide (num1 , num2 ))
8699 break
87100 else :
88101 print ("Invalid Input" )
0 commit comments