1+ def add (a , b ):
2+ """
3+ Adds two numbers together.
4+
5+ Args:
6+ a (int): The first number.
7+ b (int): The second number.
8+
9+ Returns:
10+ int: The sum of the two numbers.
11+ """
12+ return a + b
13+
14+ def subtract (a , b ):
15+ """
16+ Subtracts the second number from the first.
17+
18+ Args:
19+ a (int): The first number.
20+ b (int): The second number.
21+
22+ Returns:
23+ int: The difference of the two numbers.
24+ """
25+ return a - b
26+
27+ def multiply (a , b ):
28+ """
29+ Multiplies two numbers together.
30+ Args:
31+ a (int): The first number.
32+ b (int): The second number.
33+ Returns:
34+ int: The product of the two numbers.
35+ """
36+ return a * b
37+
38+ def divide (a , b ):
39+ """
40+ Divides the first number by the second.
41+ Args:
42+ a (int): The first number.
43+ b (int): The second number.
44+ Returns:
45+ float: The quotient of the two numbers.
46+ Raises:
47+ ZeroDivisionError: If the second number is zero.
48+ """
49+ if b == 0 :
50+ raise ZeroDivisionError ("division by zero" )
51+ return a / b
52+
53+ def main ():
54+ """
55+ Main function to demonstrate the calculator operations.
56+ """
57+ print ("Simple Calculator" )
58+ print ("1. Add" )
59+ print ("2. Subtract" )
60+ print ("3. Multiply" )
61+ print ("4. Divide" )
62+ choice = input ("Enter your choice (1-4): " )
63+
64+ a = float (input ("Enter first number: " ))
65+ b = float (input ("Enter second number: " ))
66+
67+ if choice == '1' :
68+ print (f"{ a } + { b } = { add (a , b )} " )
69+ elif choice == '2' :
70+ print (f"{ a } - { b } = { subtract (a , b )} " )
71+ elif choice == '3' :
72+ print (f"{ a } * { b } = { multiply (a , b )} " )
73+ elif choice == '4' :
74+ try :
75+ print (f"{ a } / { b } = { divide (a , b )} " )
76+ except ZeroDivisionError as e :
77+ print (e )
78+ else :
79+ print ("Invalid choice." )
80+
81+ if __name__ == "__main__" :
82+ main ()
0 commit comments