【问题标题】:Python: Pig Latin FunctionPython:猪拉丁函数
【发布时间】:2014-04-01 06:37:08
【问题描述】:

我在课堂上遇到了一个非常棘手的问题,我需要在 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

任何建议都有帮助

【问题讨论】:

标签: python string for-loop uppercase capitalization


【解决方案1】:
# 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"

这是一个词。拆分成单词应该很容易。

编辑:脑残。

【讨论】:

  • 使用if astring[0] not in "aeiouAEIOU": 代替.find() 方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 2017-07-28
相关资源
最近更新 更多