Write a program that asks for 'name' from the user and then asks for a number and stores the two in a dictionary (called 'the_dict') as key-value pair. The program then asks if the user wants to enter more data (More data (y/n)? ) and depending on user choice, either asks for another name-number pair or exits and stores the dictionary key, values in a list of tuples and prints the list. Note: Ignore the case where the name is already in the dictionary. Example: Name: pranshu Number: 517-244-2426 More data (y/n)? y Name: rich

Respuesta :

Answer:

#Declare the variables.

the_dict = {}

dictlist = []

#Begin the while loop.

while True:

   #Prompt the user to enter the name

   #and the number.

   input_name = input("Name: ")

   input_number = input("Number: ")

   

   #Ask the user to continue or stop

   #the program.

   input_choice = input('More data (y/n)? ')

   the_dict[input_name] = input_number

   

   #Check the input.

   if input_choice == 'n':

       break

#If the user want to continue

#then append in the list.

for key, value in the_dict.items():

   

   #Store the values in dictionary.

   temp_val = (key,value)

   dictlist.append(temp_val)

   

#Sort the list.

print(sorted(dictlist))

ACCESS MORE