In: Computer Science
PYTHON
Modify the program in section
Ask the user for a first name and a last name of several people.
Use a loop to ask for user input of each person’s first and last names
Each time through the loop, use a dictionary to store the first and last names of that person
Add that dictionary to a list to create a master list of the names
Example dictionary:
aDict = { "fname":"Douglas", "name":"Lee" }
Example master list, each element in the list is a dictionary:
namesList = [
{ "fname":"Douglas", "lname":"Lee" },
{ "fname":"Neil", "lname": "Armstrong" },
{ "fname":"Buzz", "lname":"Aldrin" },
{ "fname":"Eugene", "lname":"Cernan"},
{ "fname":"Harrison", "lname": "Schmitt"}
]
Pseudocode:
1) Ask for first name
2) Ask for last name
3) Create a dictionary with first name and last name
4) Add that dictionary to the master list object
5) If more names, go to (1)
6) If no more names, print the master list in a visually pleasing manner
# loop till no more names
master_list = []
while(True):
# ask for first name
fname = input("Enter first name: ")
lname = input("Enter last name: ")
# create dictionary
name_dict = {"fname": fname, "lname": lname}
# add to master list
master_list.append(name_dict)
# ask for more names
choice = int(input("Press 1 if no more names: "))
if choice == 1:
break
# print master list
print(master_list)
.
Screenshot:
Output:
.