Reputation: 29
I try to write a program with a function to capitalize every first letter in expression with the addition of one dot. For example if I write hello world
the result must be H.W.
.
My program is:
def initials(Hello World):
words = input.split(' ')
initials_words = []
for word in words:
title_case_word = word[0].upper()
initials_words_words.append(title_case_word)
output = '. '.join(initials_words)
return (initials_words)
The compilers seems that does nootexit any error but when I try to give an exression such as:print (initials(Hello World)
the compiler does not give me any result.
Upvotes: 0
Views: 235
Reputation: 152647
I identified several problems:
You need to change your function signature to take a parameter called input
. Because that's the variable you split
. NB: input
is also a built-in function so using a different variable name would be better.
Then you use initial_words_words
instead of initial_words
inside the loop.
You assign output
but you don't use it, it should probably be outside the loop and also returned.
Not an issue but you don't need (
and )
when returning.
So a changed program would look like this:
def initials(my_input):
words = my_input.split(' ')
initials_words = []
for word in words:
title_case_word = word[0].upper()
initials_words.append(title_case_word + '.')
output = ''.join(initials_words) # or ' '.join(initials_words) if you want a seperator
return output
print(initials('Hello World')) # H.W.
Upvotes: 2
Reputation: 2459
This will do it:
def initials(input_text):
return "".join(["%s." % w.upper()[0] for w in input_text.split()])
Upvotes: 2
Reputation: 2044
I see an error when trying to run your code on the 6th line: initials_words_words.append(title_case_word)
.
NameError: name 'initials_words_words' is not defined
After Fixing that, the program worked fine. Try changing it to initials_words.append(title_case_word)
Upvotes: -1