正如您将在下面的代碼中看到的那樣,我已經制作了一個程式,将英語翻譯成Pig Latin.它遵循兩個規則:
>如果單詞以元音開頭,則應附加“way”(例如:apple成為appleway)
>如果單詞以輔音序列開頭,則此序列應移至末尾,字首為“a”,後跟“ay”(例如:請成為easeaplay)
我知道這是一個奇怪的方法,但隻是幽默我.
問題:當翻譯成英文時,我想不出一種方法讓代碼知道它應該将原始單詞的根與字尾分開,因為有些單詞以1輔音開頭,其他單詞有2輔音,等等
任何幫助,将不勝感激.請記住我是新手.
vowels = ('AEIOUaeiou')
def toPigLatin(s):
sentence = s.split(" ")
latin = ""
for word in sentence:
if word[0] in vowels:
latin += word + "way" + " "
else:
vowel_index = 0
for letter in word:
if letter not in vowels:
vowel_index += 1
continue
else:
break
latin += word[vowel_index:] + "a" + word[:vowel_index] + "ay" + " "
return latin[:len(latin) - 1]
def toEnglish(s):
sentence = s.split(" ")
english = ""
for word in sentence:
if word[:len(word) - 4:-1] == 'yaw':
english += word[:len(word) - 3] + " "
else:
#here's where I'm stuck
return english
提前緻謝!