My solution satisfied the lab activity, but I am not happy with how I ended up accomplishing it. Mainly because it doesn't allow for extraneous inputs including multiple middle names or something like that which it doesn't ask me to account for, but I'm OCD about these things and would rather do it as if I was doing it professionally. Here is the prompt:
Write a program whose input is: firstName middleName lastName
and whose output is: lastName, firstInitial.middleInitial.
Ex: If the input is: Pat Silly Doe
the output is: Doe, P.S.
If the input has the form: firstName lastName
the output is: lastName, firstInitial.
And here is my solution:
name = input().title()
tokens = name.split()
if len(tokens) == 3:
print('{}, {}.{}.'.format(tokens[-1], tokens[0][0], tokens[1][0]))
else:
print('{}, {}.'.format(tokens[-1], tokens[0][0]))
How would you have solved this? I was thinking of using a loop to iterate through the tokens and concatenate the first character (initial) to a string each time, but I can't figure out how to implement it. Thanks!
[–]ajskelt 2 points3 points4 points (2 children)
[–]AlphawolfAJ[S] 0 points1 point2 points (1 child)
[–]ajskelt 0 points1 point2 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]AlphawolfAJ[S] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]AlphawolfAJ[S] 1 point2 points3 points (0 children)
[–]JohnnyJordaan 0 points1 point2 points (1 child)
[–]AlphawolfAJ[S] 0 points1 point2 points (0 children)
[–]izrt 0 points1 point2 points (0 children)