2
2
def add (x , y ):
3
3
return x + y
4
4
5
- def subtract (x , y ):
6
- return x - y
7
-
8
- def multiply (x , y ):
9
- return x * y
10
-
11
- def divide (x , y ):
12
- return x / y
13
-
14
- # Display the options to the user
15
- print ("Select operation:" )
16
- print ("1. Add" )
17
- print ("2. Subtract" )
18
- print ("3. Multiply" )
19
- print ("4. Divide" )
20
-
21
- while True :
22
- # Take input from the user
23
- choice = input ("Enter choice (1/2/3/4): " )
24
-
25
- # Check if the choice is one of the four options
26
- if choice in ('1' , '2' , '3' , '4' ):
27
- try :
28
- num1 = float (input ("Enter first number: " ))
29
- num2 = float (input ("Enter second number: " ))
30
- except ValueError :
31
- print ("Invalid input. Please enter a number." )
32
- continue
33
-
34
- if choice == '1' :
35
- print (f"{ num1 } + { num2 } = { add (num1 , num2 )} " )
36
- elif choice == '2' :
37
- print (f"{ num1 } - { num2 } = { subtract (num1 , num2 )} " )
38
- elif choice == '3' :
39
- print (f"{ num1 } * { num2 } = { multiply (num1 , num2 )} " )
40
- elif choice == '4' :
41
- print (f"{ num1 } / { num2 } = { divide (num1 , num2 )} " )
42
-
43
- # Check if the user wants another calculation
44
- next_calculation = input ("Do you want to perform another calculation? (yes/no): " )
45
- if next_calculation .lower () != 'yes' :
46
- break
47
- else :
48
- print ("Invalid input" )
49
-
5
+ def subtract (x , y ):
6
+ return x - y
7
+
8
+ def multiply (x , y ):
9
+ return x * y
10
+
11
+ def divide (x , y ):
12
+ # Prevent division by zero
13
+ if y == 0 :
14
+ return "Error: Division by zero is not allowed"
15
+ return x / y
16
+
17
+ # Display the options to the user
18
+ def display_menu ():
19
+ print ("\n Select operation:" )
20
+ print ("1. Add" )
21
+ print ("2. Subtract" )
22
+ print ("3. Multiply" )
23
+ print ("4. Divide" )
24
+
25
+ # Main program loop
26
+ while True :
27
+ display_menu ()
28
+
29
+ # Take input from the user
30
+ choice = input ("Enter choice (1/2/3/4): " )
31
+
32
+ # Check if the choice is valid
33
+ if choice in ('1' , '2' , '3' , '4' ):
34
+ try :
35
+ num1 = float (input ("Enter first number: " ))
36
+ num2 = float (input ("Enter second number: " ))
37
+ except ValueError :
38
+ print ("Invalid input. Please enter numeric values." )
39
+ continue
40
+
41
+ # Perform the chosen operation
42
+ if choice == '1' :
43
+ print (f"{ num1 } + { num2 } = { add (num1 , num2 )} " )
44
+ elif choice == '2' :
45
+ print (f"{ num1 } - { num2 } = { subtract (num1 , num2 )} " )
46
+ elif choice == '3' :
47
+ print (f"{ num1 } * { num2 } = { multiply (num1 , num2 )} " )
48
+ elif choice == '4' :
49
+ print (f"{ num1 } / { num2 } = { divide (num1 , num2 )} " )
50
+
51
+ # Check if the user wants another calculation
52
+ next_calculation = input ("Do you want to perform another calculation? (yes/no): " ).lower ()
53
+ if next_calculation != 'yes' :
54
+ print ("Exiting the calculator." )
55
+ break
56
+ else :
57
+ print ("Invalid input. Please select a valid operation." )
0 commit comments