-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu_driven.py
More file actions
45 lines (39 loc) · 1.39 KB
/
menu_driven.py
File metadata and controls
45 lines (39 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def add_numbers():
try:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
result = num1 + num2
print(f"The result of adding {num1} and {num2} is {result}.")
except ValueError:
print("Invalid input! Please enter numeric values.")
def subtract_numbers():
try:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
result = num1 - num2
print(f"The result of subtracting {num2} from {num1} is {result}.")
except ValueError:
print("Invalid input! Please enter numeric values.")
def display_message():
print("Hello! Welcome to the Menu-Driven Program.")
def main_menu():
while True:
print("\nMenu:")
print("1. Add Numbers")
print("2. Subtract Numbers")
print("3. Display a Message")
print("4. Exit")
choice = input("Enter your choice (1-4): ")
if choice == '1':
add_numbers()
elif choice == '2':
subtract_numbers()
elif choice == '3':
display_message()
elif choice == '4':
print("Exiting the program. Goodbye!")
break
else:
print("Invalid choice! Please select a valid option.")
if __name__ == "__main__":
main_menu()