58.6k views
0 votes
9.17 LAB: Name format Many documents use a specific format for a person's name. Write a program whose input is: firstName middleName lastName, and whose output is: lastName, firstName middleInitial. Ex: If the input is: Pat Silly Doe the output is: Doe, Pat S. If the input has the form firstName lastName, the output is lastName, firstName. Ex: If the input is: Julia Clark the output is: Clark, Julia

User Hello Lili
by
5.0k points

2 Answers

4 votes

Answer:

A better answer is:

#get input and assign to variable

name = input()

#split input in new variable

split_name = name.split()

#split first name

firstName = split_name[0]

#split middle name

middleName = split_name[1]

#check how many 'names' in list

if len(split_name) == 2:

#last name, first initial

print(middleName + ',', firstName[0:1] + '.')

# check how many 'names' in list again

if len(split_name) == 3:

#assign last name

lastName = split_name[2]

#print last name, firstinitial.middleinitial

print(lastName + ',', firstName[0:1] + '.' + middleName[0:1] + '.')

Step-by-step explanation:

User Alsadk
by
5.2k points
2 votes

Answer:

name=input("Enter a name: ")

names = name.split()

if

(len(names)==3): middle=names[1] mid=middle[0].upper()+"." last=names[2]+"," first=names[0] print('Formatted Name:',last,first,mid) elif(len(names)==2): last=names[1]+',' first=names[0]

print('Formatted Name:',last,first)

else:

print('The input does not have the correct form')

User Shubho Shaha
by
4.8k points