-2

If input is: Jane Silly Doe Output is: Doe, J.S.

If input is: Julia Clark Output is: Clark, J. Issue:

I can make it run for either or but not both

This only works for input Jane Silly Doe but not for input Julia Clark. I can make it run for either or but not both at the same time.

string = input()
words = string.split()
title = ''
for word in words:
    title += word

another = (words[-1]+',')
second = (words[0])
third = (words[1])
fourth = second + third

upper = ''
for char in fourth:
    if char.isupper():
        upper += char

join_string = '.'.join(upper)
print(another, join_string + '.')
azro
  • 53,056
  • 7
  • 34
  • 70
Dlal529
  • 13
  • 2

1 Answers1

0

You can use fixed index like words[1] use slicing words[:-1] to get all values except the last one

def shorten(string):
    words = string.split()
    result = words[-1] + ', '
    for char in ''.join(words[:-1]):
        if char.isupper():
            result += char + "."
    print(result)
shorten("Julia Clark")  # Clark, J.
shorten("Jane Silly Doe")  # Doe, J.S.
shorten("Jane Silly Jack James Doe")  # Doe, J.S.J.J.
azro
  • 53,056
  • 7
  • 34
  • 70
  • 1
    Yes that worked thank you! I did return(result) Then print(shorten(string)) unindented at the end – Dlal529 Jul 16 '22 at 22:56
  • @Dlal529 You may think about [accepting an answer](https://stackoverflow.com/help/someone-answers) to reward those how helped you, or at least comment to explain what's missing ;) – azro Jul 17 '22 at 08:03