Python learning --- string processing

This world is but a canvas to our imagination.

The world is just a canvas for our imagination. ----Apri 22

'''
Title content:
"Pig Latin" is an English children's text rewriting game. The whole game follows the following rules:

(1). The vowels are 'a', 'e', ​​'i', 'o', 'u'. The letter 'y' is also treated as a vowel if it is not the first letter. All other letters are consonants. For example, the word "yearly" has three vowels ('e', 'a', and the last 'y') and three consonants (the first 'y', 'r', and 'l').

(2). If an English word starts with a vowel, add "hay" at the end of the word to get the word corresponding to "Pig Latin". For example, "ask" becomes "askhay" and "use" becomes "usehay".

(3). If the English word starts with the letter 'q' and is followed by the letter 'u', move "qu" to the end of the word and add "ay" to get the word corresponding to "Pig Latin". For example, "quiet" becomes "ietquay" and "quay" becomes "ayquay".

(4) If an English word starts with a consonant letter, move all consecutive consonant letters to the end of the word and add "ay" to get the word corresponding to "Pig Latin". For example, "tomato" becomes "omatotay", "school" becomes "oolschay", "you" becomes "ouyay", "my" becomes "ymay", and "ssssh" becomes "sssshay".

(5). If there are uppercase letters in English words, all letters must be converted to lowercase.

Input format:
a series of words separated by spaces.

Output format:
Convert each word according to the above rules, separate words with spaces.

Input sample:
Welcome to the Python world Are you ready

Sample output:
elcomeway otay ethay ythonpay orldway arehay ouyay eadyray
'''

def is_vowel(c):
    ###判断是元音字符
    return c in ['a','e','i','o','u']

#print(is_vowel('y'))
###输入
string = input()
###初步处理
string = string.lower()
string = string.strip()
words = string.split()
###临时存储列表
change_string = []
for word in words:
    first = word[0]
    if is_vowel(first):
        ###首字母是元音
        word = word[:] + "hay"
    else:
        ###首字母不是元音
        if word[:2] =="qu":
            ###前两字是“qu”
            word = word[2:] + "quay"
        else:
            for i in range(1,len(word)):
                ###对每个WORD,遍历字符
                if is_vowel(word[i]) or (word[i] =='y'):
                     break
                else:
                    first = first + word[i]
            word = word[len(first):] + first + "ay"
    ###将改写的WORD 存入临时列表
        change_string.append(word)
for word in change_string:
    print(word,end=" ")
print()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324650775&siteId=291194637