I want to filter out words with one letter in a string (pig latin translator) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: I want to filter out words with one letter in a string (pig latin translator) (/thread-23603.html) |
I want to filter out words with one letter in a string (pig latin translator) - po0te - Jan-08-2020 How to create a function to ignore words with one letter in a string? I am trying to make a pig latin translator that can translate sentences but leaves words like "a" be. I've tried what I listed below, and get no errors, but it still adds "ay" on the end of one letter words. Please let me know what I'm doing wrong! def piglatin(sentence): while len(sentence.split(' ')) > 1: return ' '.join(map( lambda str: str + 'ay' if str[0] in 'aeiou' else str[1:] + str[0] + 'ay', sentence.split(' ') )).capitalize() if len(sentence.split()) < 1: breakI'm also not sure if break should go at the end of loop. For more explanation, this is what my program is doing now: input: climb a tree output: Limbcay aay reetay What I want it do do: output: Limbcay a reetay RE: I want to filter out words with one letter in a string (pig latin translator) - perfringo - Jan-08-2020 If I understand correctly 'the translation' means that first character is moved to an end and 'ay' is added to all words which are longer than 1 letter. One possibility to create translate function and then use it on all words in sentence. def translate(word): if len(word) == 1: return word else: return f'{word[1:]}{word[0].lower()}ay'Then just: >>> sentence = 'Climb a tree' >>> ' '.join([translate(word) for word in sentence.split()]) 'limbcay a reetay'This must be further developed. It will not work as expected with the punctuation at the end of sentence and there is no rules for capitalisation of words. |