Kristina D.
asked 10/14/20Creating a Name-Sorting Program in Python
Hi I really need help in my programming class with this assignment!!!
Write a function named split_name which takes one parameter called name. If the name parameter value is a name in the form Lastname, FirstName (e.g., ”Grounds, Nic”) then the function should return a list of two elements where FirstName is the first element and LastName is the second element. If the name parameter value is a name in the form FirstName LastName (e.g., ”Nic Grounds”) then the function should return a list of two elements where FirstName is the first element and LastName is the second element. Repeatedly prompt the user for a name (which the user may give in LastName, FirstName form, or FirstName LastName form). If the user’s response is an empty string (i.e., they simply press Return) cease looping. If the user’s response is not the empty string, call the split_name function you wrote and append the returned value to a list (initially empty). Using the sorted function (which can take a single parameter, a list) and returns a copy of the list in sorted order) sort the list of names once the loop has finished. Print the sorted list of names, one name per line in LastName, FirstName form.
1 Expert Answer

Patrick B. answered 10/14/20
Math and computer tutor/teacher
print("Hello ")
name_str = '?'
name_list = { 'name1' : 'name2'}
while len(name_str)>0:
name_str = input('Please input the name :>')
if len(name_str)==0:
break
name1,name2 = name_str.split()
name_list[name1]=name2
print(name_list)
#sorts by name2
sort_names = sorted(name_list.items(), key=lambda x: x[1], reverse=False)
print(sort_names)
for i in sort_names:
print(i[0], i[1])
#sorts by name1
sort_names2 = sorted(name_list.items(), key=lambda x: x[0], reverse=False)
print(sort_names2)
for i in sort_names2:
print(i[0], i[1])
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Kristina D.
Sample potential execution of a program meeting the requirements: Welcome to my name-sorter program. Enter names in either "lastname, firstname" or "firstname lastname" form. When you are done entering names, enter nothing and press Return. Jim Halpert Pam Beasley Martinez, Oscar Michael Scott Dwight Schrute Martin, Angela Malone, Kevin Creed Bratton The sorted list of names is: Martin, Angela Bratton, Creed Schrute, Dwight Halpert, Jim Malone, Kevin Scott, Michael Martinez, Oscar Beasley, Pam10/14/20