143,110 views
7 votes
7 votes
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: pransh

User Jrovegno
by
2.8k points

1 Answer

7 votes
7 votes

Answer:

Step-by-step explanation:

The following is written in Python and does as the question requested. It asks the user for the information and adds it to the dictionary (the_dict) then it asks the user if they want to add more info and keeps adding to the_dict until the user states no. Once that happens the program places the dictionary into a tuple and prints out the results.

the_dict = {}

test_tup = ()

while True:

key = input("Please enter a name: ")

value = input("Please enter a number: ")

the_dict[key] = value

answer = input("More data (y/n)?").lower()

if answer != 'y':

break

test_tup = list(test_tup)

test_tup.append(the_dict)

test_tup = tuple(test_tup)

print(test_tup)

User Stagg
by
3.2k points