(calle iry") input argume The function aims to create a dictionary, to do that, the function needs to ask the use to enter how many key-value pair to create. Then the user needs to enter (input0) each key-value pair using the keyboard input. The function needs to take the first elements as a key, the second element entered as a value and add it to the "dictionary".


def create_a_dictionary(): # Defining method create_a_dictionary()
Dictionary={} # Creating empty dictionary
n=int(input("How many key-value pairs do you want to create? ")) #Taking input for number of key value pairs
print("Enter",n,"key value pairs:") #Printing statement
for i in range(n): #Loop to take input for n number of key value pairs
List=input().split() # Storing user input in List
Dictionary[List[0]]=List[1] #Adding element to dictionary
return Dictionary # Returning Dictionary
def reverse_dictionary(Dictionary): # Defining method reverse_dictionary() with Dictionary as input
return dict(zip(Dictionary.values(),Dictionary.keys())) # Returning Dictionary by swapping key value pairs.
Dictionary=create_a_dictionary() # Creating dictionary by calling create_a_dictionary()
ReverseDict=reverse_dictionary(Dictionary) # Reversing dictionary by calling reverse_dictionary()
print("Initial dictionary is: ",Dictionary) # Printing initial dictionary
print("Dictionary after reversal is:",ReverseDict) # Printing reversed dictionary
Step by step
Solved in 2 steps with 1 images









