Oct-08-2019, 06:07 AM
(This post was last modified: Oct-08-2019, 06:13 AM by frenchyinspace.)
I'm midway through my first semester of computer science. I've been staying as far ahead of the homework as possible because I find it fun and challenging. But this problem has stumped me like no other. I do not have much experience with lists and manipulating them and I suspect I've made this a lot harder for myself than I needed to. Couldn't find any info on how to do this online and I need some guidance. Thanks in advance. Assignment below:
Write a program that prompts the user to enter a list of names. Each person's name is separated from the next by a semi-colon and a space ('; ') and the names are entered lastName, firstName (i.e. separated by ', '). Your program should then print out the names, one per line, with the first initial of the first name, followed by ".", and followed by the last name.
A sample run of your program should look like:
Please enter your list of names: Cohn, Mildred; Dolciani, Mary P.; Rees, Mina; Teitelbaum, Ruth; Yalow, Rosalyn
You entered:
M. Cohn
M. Dolciani
M. Rees
R. Teitelbaum
R. Yalow
Thank you for using my name organizer!
Here's what I have so far:
1. having split the original list into sublists, how to further split the names into first and last name.
Originally I had name2=name1[0].split"," but that didn't work for looping as the index had to be specified.
2. I was able to reverse the order of each full name using name3=name2[-1::-1], but that no longer works.
3. Need to join them back together, this also doesn't work
Thanks,
Write a program that prompts the user to enter a list of names. Each person's name is separated from the next by a semi-colon and a space ('; ') and the names are entered lastName, firstName (i.e. separated by ', '). Your program should then print out the names, one per line, with the first initial of the first name, followed by ".", and followed by the last name.
A sample run of your program should look like:
Please enter your list of names: Cohn, Mildred; Dolciani, Mary P.; Rees, Mina; Teitelbaum, Ruth; Yalow, Rosalyn
You entered:
M. Cohn
M. Dolciani
M. Rees
R. Teitelbaum
R. Yalow
Thank you for using my name organizer!
Here's what I have so far:
nlist= input("Enter list of names like this: Bastien, Sacha; Smith, Adam") name1=nlist.split("; ") #splits the list into sublists of [lastname, firstname] name2=[item.split(",") for item in name1] for item in range(len(name1)): name2= [item.split(",") for item in name1] name3=name2[-1::-1] name4=" ".join(name3) print(name4[0]+". "+name2[0])I've run into many problems here.
1. having split the original list into sublists, how to further split the names into first and last name.
Originally I had name2=name1[0].split"," but that didn't work for looping as the index had to be specified.
2. I was able to reverse the order of each full name using name3=name2[-1::-1], but that no longer works.
3. Need to join them back together, this also doesn't work
Thanks,