def get_words_by_first_letter(words, letter):
for word in words:
if word[0] == letter:
return word
You need to put the entire code between triple backtick lines. Without that, quotes get curlified.
Anyway, here is a solution in the style you used, and an alternative using a list comprehension.
The latter is likely too advanced for you now, but something you should learn in the future.
Both print ['apple', 'and']
and omit 'or'
.
def get_words_by_first_letter(words, letter):
"""Return a list of all words that start with the given letter."""
hits = []
for word in words:
if word[0] == letter:
hits.append(word)
return hits
def gwbfl(words, letter):
return [word for word in words if word[0] == letter]
print(get_words_by_first_letter(["apple" , "and", 'or'], "a"))
print(gwbfl(["apple" , "and", 'or'], "a"))
Thank you, Terry Jan Reddy. Appreciate your reply.