Learn Python 013: Pig Latin Translator

# get sentence from user
original = input('Please enter the sentence you want to translate: ').strip().lower()
# split sentence to words
words = original.split()
# loop through words and translate to pig latin
new_words = []
for word in words:  # if the word start with vowel, add 'yay' at the end
    if word[0] in 'aeiou':
        new_word = word + 'yay'
        new_words.append(new_word)
    else:
        vowel_position = 0
        for letter in word:  # otherwise, move the first consonant cluster to to end and add 'ay'
            if letter not in 'aeiou':
                vowel_position = vowel_position + 1
            else:
                break
        consonants = word[:vowel_position]
        the_rest = word[vowel_position:]
        new_word = the_rest + consonants + 'ay'
        new_words.append(new_word)
# stick words back together
output = ' '.join(new_words)
# print the output
print(output)

 

转载于:https://www.cnblogs.com/mxyzptlk/p/7191848.html

你可能感兴趣的:(Learn Python 013: Pig Latin Translator)