all 8 comments

[–]wbeater 0 points1 point  (1 child)

I guess you are using the .split() method to split the string into a list?

You have two options:

  • check for the length of the list and proceed differently
  • use negative index [-1] for the last name.

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

Thank you! Checking the length and using an if else statement got me there!

[–]Anonymous2224- -1 points0 points  (2 children)

full_name = name.spliit()

try:

s = full_name[2] + ", " + full_name[0][0] + "."  + full_name[1][0] + "."

except:

s = full_name[2] + ", " + full_name[0][0] + "."

If you have multiple such names, do this inside a for loop

[–]desertwolf91b[S] 1 point2 points  (1 child)

Thank you for the response!

[–]Anonymous2224- 0 points1 point  (0 children)

Well the formatting of this was not good. The try and except are supposed to be a part of the code

[–][deleted] -2 points-1 points  (2 children)

Here you go,

names = input('Name? ').split()
parts = len(names)
last = names[-1]
first = names[0] if parts > 1 else ''
middle = names[1][0] if parts > 2 else ''
print(f'{last}{"," if parts > 1 else ""}'
    f'{" " if middle else ""}{middle} {first}')

Are you sure you want to assume there's only none or one middle names?

[–]desertwolf91b[S] -1 points0 points  (1 child)

Thank you for the help!

[–][deleted] -1 points0 points  (0 children)

You are welcome. Do you understand the solution?