Python:Pig拉丁函数

2024-09-30 02:26:18 发布

您现在位置:Python中文网/ 问答频道 /正文

对于我的类,我遇到了一个很难回答的问题,我需要用Python为给定的字符串创建一个Pig-Latin转换器。在

基本上,规则如下。在

  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

任何建议都是有帮助的


Tags: 字符串in功能fordef单词末尾元音
1条回答
网友
1楼 · 发布于 2024-09-30 02:26:18
# 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"

这是一句话。分词应该很容易。在

编辑:脑屁。在

相关问题 更多 >

    热门问题