Python: A program that receives a name and outputs it by family name (e.g., Kim, Lee, Park...)

Asked 2 weeks ago, Updated 2 weeks ago, 3 views

I'm learning Python on my own. I organized it up to here, but I want to distinguish the end of the entry when I type the enter key without entering the number of participants Whenever one's name is added, I want to print out a dictionary and check it.

Output example)

Enter the name of the participant (enter key to exit): Park Joon-hee
{'Park':'Park Junhee'}
Enter the name of the participant (enter key to exit): Kim Sung-yong
{'Park':['Park Jun-hee'],'Kim':['Kim Sung-yong']}
Enter the name of the participant (enter key to exit): Junyoung Kim
{'Park':['Park Jun-hee'],'Kim':'Kim Sung-yong','Kim Jun-young'}
Enter the name of the participant (enter key to exit):

Attendance list: 
Park: Park Junhee
Kim: Kim Sung-yong, Kim Joon-young
attendanceList = []
attendanceCount = int (input ("Enter the number of attendees: ")))
for i in range(attendanceCount) :
    name = input ("Enter the name of the participant: ")
    For the same Family Names in Attendance List: # Repeat the last name list
        If name[0] == sameFamilyNames[0][0]: # If the last name is the same last name as the given name,
            sameFamilyNames.append(name) # Add a name to the same family name list
            name = " # Indicates that you have added your family name to the family name
            break
    If name : # name!=" If you have never been added to the family name list,
        AttendanceList.append([name]) # Create and add a new family name list
    print(attendanceList)

print ("Attendance list: ")
for sameFamilyNames in attendanceList :
    print(sameFamilyNames[0][0], end="")
    separator = ":"
    for name in sameFamilyNames :
        print(separator, name, end="")
        separator = ","
    print()

python

2022-09-20 10:21

1 Answers

Well, I think I'd do this.

from pprint import pprint

dict_a = {}

while 1:
    a = input('Please enter a name. (Exit if there is no name): ')
    if a.strip() == '':
        break
    if dict_a.get(a[0]):
        dict_a[a[0]].append(a)
    else:
        dict_a[a[0]] = [a]
   print(a[0] + 'seed list :', dict_a[a[0]])

print ('Start attendance list')
# # pprint(dict_a)
for i in dict_a:
    print(i, 'Mr. List')
    print(','.join(dict_a[i])

print ('End of Attendance List')


2022-09-20 10:21

If you have any answers or tips


© 2022 pinfo. All rights reserved.