Why does the program fail?
Have a closer look at your cap()
function.
def cap(words):
for j in words:
print(j)
return j.capitalize()
return j.captialize()
will exit the function and will only return the capitalized value of the first word.
Correction
The function must capitalize all the elements in the list.
def cap(words):
capitalized = []
for word in words:
capitalized.append(word.capitalize())
return capitalized
Now the final code should look like
def cap(words):
capitalized = []
for word in words:
capitalized.append(word.capitalize())
return capitalized
words = ["hello","foo","bar"]
capitalized = cap(words)
print(*capitalized)
perhaps a more pythonic way would be to use a list comprehension
def cap(words):
return [word.capitalize() for word in words]
words = ["hello","foo","bar"]
capitalized = cap(words)
print("Regular: ",*words) # printing un-packed version of words[]
print("Capitalized: ",*capitalized)
Output:
Regular: hello foo bar
Capitalized: Hello Foo Bar