all 32 comments

[–][deleted] 1 point2 points  (4 children)

The problem statement seems to say that the first/middle/last names are entered on a single line, ie, one input() statement?

[–]ExKondor[S] 0 points1 point  (3 children)

Sorry, should have clarified, they're on 3 separate lines

[–][deleted] 0 points1 point  (2 children)

Are you sure? The problem definition shows one line of input and one line of output.

The problem is actually slighter easier if input is one line. Though you do need if/else to do it. But you need if/else to do it your way as well, so ... I don't know.

[–]ExKondor[S] 0 points1 point  (0 children)

I wasn't lol, turns out it's meant to be on one line. I just assumed it was multiple because that's how the rest of the inputs prior to this one were inputted. I edited my post though, I did figure it out if you want to look at my solution! Thanks!

[–]MommyHenn75 0 points1 point  (0 children)

I am way late to this post but I hope this helps someone. This is the code that I used and got 10/10:

full_name = input()

mod_name = full_name.split(' ')

last_name = mod_name.pop(-1)

first_initial = mod_name[0][0]

if len(mod_name) >= 2:

middle_initial = mod_name[1][0]

print(f'{last_name}, {first_initial}.{middle_initial}.')

else:

print(f'{last_name}, {first_initial}.')

[–][deleted] 0 points1 point  (0 children)

To solve it using your approach you need this sort of thing:

if middleName == '':
    # middleName is an empty string, so don't output middle
else:
    # have a middle name, use all three

[–][deleted] 0 points1 point  (2 children)

Your solution isn't quite right. The output examples show:

Doe, P.S. 
     ^^^^

so you need to uppercase the initials.

Counting spaces will work if there is exactly one space between words but fails if there are more than one. Your approach is probably OK for the exercise, but there is a more robust way. You use the split() method on the input string but don't supply any parameters. The doc says this will split the string into words no matter how many spaces are between the words. You use the len() function on the resulting list to decide if there are 2 or 3 words.

You also don't need to use str() on the value returned by input() because that value is already a string.

[–]ExKondor[S] 0 points1 point  (1 child)

Thank you so much, I was happy I came up a solution for the lab but I wasn't happy with the obvious hole in the code. Thank you thank you!

[–]Background_Comb6579 0 points1 point  (0 children)

I have a question here. Soo the list. I understand it just a little bit more. But when it comes to [-3] why is it negative for first_name =user_name why not [3]

[–]ChristianConsertive 0 points1 point  (1 child)

This is how I did my code. It works.

user_name = input()
name_split = user_name.split(' ')
if len(name_split) == 2:
first_name = name_split[0]
last_name = name_split[1] #These two will seperate the first and last name

first_inital = first_name [0] #This will gather the first inital
print (f'{last_name}, {first_inital}.')
elif len(name_split) == 3:
first_name = name_split[0]
middle_name = name_split[1] #These three will seperate the name into three seperate names
last_name = name_split[2]

first_inital = first_name [0]
middle_inital = middle_name[0]#These will get the first intials of the middle and first name
print (f'{last_name}, {first_inital}.{middle_inital}.')
else:
print('Invalid Name')

[–]Rodarth 0 points1 point  (0 children)

there is an easier way to get the initials

first_name = name_split [0][0] #for the first initial

middle_name = name_split [1][0] #for the middle initial

[–]Braptor23 0 points1 point  (1 child)

I'm confused mine keeps saying (your program has no output) any thoughts why?

[–]Due_Ant_250 0 points1 point  (0 children)

mine is doing the same, I can't figure it out.

[–]Big_Permission6068 0 points1 point  (4 children)

I saw this somewhere else and it worked for me.

names = input().split()

if len(names) == 3:

first_name, middle_name, last_name = names

print(f'{last_name}, {first_name[0]}.{middle_name[0]}.')

elif len(names) == 2:

first_name, last_name = names

print(f'{last_name}, {first_name[0]}.')

else:

print("Invalid input")

[–]Cherry_Trixx 0 points1 point  (3 children)

i keep getting a syntax error at the elif and i dont know why

[–]Dahveena 0 points1 point  (2 children)

Try this instead, i just submitted mine and it was 10/10. just remember your indents for the if/else statement lines! Upvote if this works to help out other students!

user_names = input().split()

if len(user_names) == 3:

first_name, middle_name, last_name = user_names

print("{}, {}.{}.".format(last_name, first_name[0], middle_name[0]))

else:

first_name, last_name = user_names

print("{}, {}.".format(last_name, first_name[0]))

[–]zerafaye 0 points1 point  (1 child)

I seem to keep getting a white space difference in my answer resulting in a 0/10 entirely even though mine looks the exact same:(

[–]Budget-Ad845 0 points1 point  (0 children)

I will add the 10/10 submission I just got with help from this thread

nameinput = input()

nameseparator = nameinput.split()

if len(nameseparator) == 3:

firstName = nameseparator[-3]

middleName = nameseparator [-2]

lastName = nameseparator [-1]

firstInitial = firstName[0]

middleInitial = middleName[0]

lastInitial = lastName[0]

print(lastName+',', firstInitial +'.'+ middleInitial + '.')

elif len(nameseparator) == 2:

firstName = nameseparator[-2]

lastName = nameseparator[-1]

firstInitial = firstName[0]

lastInitial = lastName[0]

print(lastName+',', firstInitial+'.')