Learn Python 013: Pig Latin Translator

本文介绍了一个简单的Pig Latin翻译器实现方法。该程序能够将输入的英文句子转换为Pig Latin形式,对于以元音开头的单词,在其后添加'yay';对于以辅音开头的单词,则将初始的辅音簇移至单词末尾并添加'ay'。
# 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值