Python: 猪拉丁文函数

-2 投票
1 回答
2050 浏览
提问于 2025-04-18 00:47

我在课堂上遇到了一个非常难的问题,我需要用Python为给定的字符串创建一个猪拉丁文转换器。

基本上,规则如下:

  1. 对于以一个或多个辅音字母开头的单词("y"也算辅音),把这些辅音字母移到单词的末尾,然后加上字符串“ay”。

  2. 对于其他单词,在末尾加上字符串“way”。

这个函数还必须处理大小写和标点符号。建议我们创建一个单独的函数来找到任何单词的第一个元音字母,并在主函数中使用它。我大致上做到了这一点,但我在把大小写和标点符号融入我的主要公式时遇到了麻烦,还有如果一个单词没有元音字母该怎么办(因为在我们的规则中,“y”不算元音,所以像“my”这样的单词就没有元音)。

这是我目前的代码。

def vowelfinder(astring):
    vowels = ["A","E","I","O","U","a","e","i","o","u"]
    alist = astring.split()
    for i in alist:
        for j in range(len(i)):
            if i[j] in vowels:
                print(j)
                break

def igpay(astring):
    vowelfinder(astring)
    for i in alist

任何建议都很有帮助。

1 个回答

0
# For any word that begins with one or more consonants (y is considered a consonant):
if "aeiouAEIOU".find(astring[0]) == -1:
    # move the consonants to the end of the word and append the string 'ay'.
    return astring[1:] + astring[0] + "ay"
# For all other words,
else:
    # append the string 'way' to the end.
    return astring + "way"

这是关于一个单词的。把它分成单词应该很简单。

编辑:突然想不起来了。

撰写回答