Write a program to find the number of key-value pairs in a dictionary
SOLUTION.
# Creating an empty dictionary
my_dict = {}
# Taking dynamic input from the user
n = int(input("Enter the number of key-value pairs: "))
# Adding key-value pairs dynamically
for i in range(n):
key = input(f"Enter key {i+1}: ")
value = input(f"Enter value for '{key}': ")
my_dict[key] = value
# Displaying the dictionary
print("\nDictionary:", my_dict)
# Finding the number of key-value pairs
num_pairs = len(my_dict)
print("Number of key-value pairs in the dictionary:", num_pairs)