python中的Pig拉丁字符串转换

新手上路,请多包涵

我正在尝试创建一个将文本转换为 pig Latin 的函数:修改每个单词的简单文本转换,将第一个字符移动到末尾并将“ay”附加到末尾。但我得到的只是一个空列表。有小费吗?

 def pig_latin(text):
  say = ""
  words = text.split()
  for word in words:
    endString = str(word[1]).upper()+str(word[2:])
    them = endString, str(word[0:1]).lower(), 'ay'
    word = ''.join(them)
    return word

print(pig_latin("hello how are you")) # Should be "ellohay owhay reaay ouyay"
print(pig_latin("programming in python is fun")) # Should be "rogrammingpay niay ythonpay siay unfay"

原文由 inkblot 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 471
2 个回答
def pig_latin(text):
  words = text.split()
  pigged_text = []

  for word in words:
    word = word[1:] + word[0] + 'ay'
    pigged_text.append(word)

  return ' '.join(pigged_text)

print(pig_latin("hello how are you"))

输出: ellohay owhay reaay ouyay

原文由 emremrah 发布,翻译遵循 CC BY-SA 4.0 许可协议

def pig_latin(text):
  say = []
  # Separate the text into words
  words = text.split(" ")
  for word in words:
    # Create the pig latin word and add it to the list
    say.append(word[1:]+word[0]+'ay')
    # Turn the list back into a phrase
  return " ".join(x for x in say)

原文由 Rohan Kumara 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题