【发布时间】:2022-08-18 15:18:04
【问题描述】:
所以这里有这段代码,解决方案已经写好了,但我无法理解它。 \'say\' 变量是如何以只有空格和索引的顺序分配文本的。以及上面的拆分方法的作用是什么。尽管我非常了解列表方法,但解决方案对我来说是模糊的。任何澄清? .
问题:
让我们创建一个将文本转换为猪拉丁文的函数:一个简单的文本 修改每个单词的转换,将第一个字符移动到 末尾并在末尾附加“ay”。例如,python 最终为 亿通支付。
编码:
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 += word[1:]+word[0]+\'ay \' # Turn the list back into a phrase return say 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\"
-
请更新代码的缩进。 Python 对缩进非常敏感,python 程序员也是如此。