Skip to content

24.Write a menu-driven Python program to perform the following operations on a dictionary like Add Key-Value Pair Update Value Delete KeySearch Key Display Dictionary Display Keys Display Values Display Items Count Total Keys Clear Dictionary Exit

ASSIGNMENT

Q.24

Q 24.Write a menu-driven Python program to perform the following operations on a dictionary like Add Key-Value Pair Update Value Delete KeySearch Key Display Dictionary Display Keys Display Values Display Items Count Total Keys Clear Dictionary Exit

# Menu-driven program to perform operations on a dictionary data = {} while True: print("\n========== DICTIONARY MENU ==========") print("1. Add Key-Value Pair") print("2. Update Value") print("3. Delete Key") print("4. Search Key") print("5. Display Dictionary") print("6. Display Keys") print("7. Display Values") print("8. Display Items") print("9. Count Total Keys") print("10. Clear Dictionary") print("11. Exit") print("=====================================") choice = int(input("Enter your choice: ")) if choice == 1: # Add Key-Value Pair key = input("Enter key: ") value = input("Enter value: ") data[key] = value print("Key-value pair added successfully.") elif choice == 2: # Update Value key = input("Enter key to update: ") if key in data: value = input("Enter new value: ") data[key] = value print("Value updated successfully.") else: print("Key not found.") elif choice == 3: # Delete Key key = input("Enter key to delete: ") if key in data: del data[key] print("Key deleted successfully.") else: print("Key not found.") elif choice == 4: # Search Key key = input("Enter key to search: ") if key in data: print("Key found.") else: print("Key not found.") elif choice == 5: # Display Dictionary print("Dictionary:", data) elif choice == 6: # Display Keys print("Keys:", data.keys()) elif choice == 7: # Display Values print("Values:", data.values()) elif choice == 8: # Display Items print("Items:", data.items()) elif choice == 9: # Count Total Keys print("Total Number of Keys:", len(data)) elif choice == 10: # Clear Dictionary data.clear() print("Dictionary cleared successfully.") elif choice == 11: # Exit print("Program terminated.") break else: print("Invalid choice. Please try again.")