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 Number: 517-842-5425 More data (y/n)? y Name: alireza Number: 517-432-5224 More data (y/n)? n [('alireza', '517-432-5224'), ('pranshu', '517-244-2426'), ('rich', '517-842-5425')]

Respuesta :

pyman

Answer:

Okay. I am doing it with Python. Hope it'll help you to understand the concept.

Code:

the_dict={}   #Creating a dictonary

def dat():    # Creating a function named dat()

   

   name = input('Your Name: ')   # Data from user

   ph = input('Phone Number: ')

   for i in range(1):  #Creating loop

       the_dict[name]=ph   # Placing the value in dictonary

   more = input('More Data (y/n): ')   # Asking for More data

   if more == 'y':   # if more is y then run the function dat()

       dat()   # calling dat()

   elif more == 'n':   # if more is n then print the dictonary named 'the_dict'

       print(the_dict)

   else:

       print('Wrong Input')  # if wrong input print the message and run dat() again

       dat()

dat()  # calling the function

   

Result:

Your Name: PyMan

Phone Number: 9814579063

More Data (y/n): y

Your Name: PyMman2

Phone Number: 451513262026

More Data (y/n): y

Your Name: C#Man

Phone Number: 7798822222

More Data (y/n): n

{'PyMan': '9814579063', 'PyMman2': '451513262026', 'C#Man': '7798822222'}

Below is the required Python code.

Python

Program:

#Creating a dictionary

the_dict={}  

#Creating a function

def dat():    

   

  #Input data

  name = input('Your Name: ')  

  ph = input('Phone Number: ')

  #Creating a loop

  for i in range(1):  

      #Placing the value in dictionary

      the_dict[name]=ph  

  #Asking the user for more data  

  more = input('More Data (y/n): ')  

  #Run the function, of more is "y"

  if more == 'y':

     

      #Calling the function dat()

      dat()  

  #Print the dictionary named "the_dict", if more is "n"

  elif more == 'n':  

      print(the_dict)

  else:

      #Print the message and run the function, if wrong

      print('Wrong Input')  

      dat()

#Function calling

dat()  

Program explanation:

  • Creating a dictionary and function.
  • Input data.
  • Creating a loop.
  • Placing the value in dictionary.
  • Asking the user for more data.
  • Run the function, of more is "y".
  • Calling the function.
  • Print the dictionary named "the_dict", if more is "n".
  • Print the message and run the function, if wrong  

Output:

Find below the attachment of program output.

Find out more information about Python here:

https://brainly.com/question/26497128

Ver imagen Cricetus