【发布时间】:2021-02-03 17:32:25
【问题描述】:
vowel = ('A','E','I','O','U','a','e','i','o','u')
sentence = 'Write a script that encodes phrases'
def pig_latin(sentence):
for words in sentence.split():
if words[0] in vowel:
words = words + 'ay'
else: words = words[1:]+ words[0] + 'ay'
return(words)
pig_latin(sentence)
我试图让这个函数打印出句子中的所有单词,但无论我将return 放在哪里,它总是只给我句子中的第一个/最后一个单词。有人可以告诉我我做错了什么吗?谢谢!
【问题讨论】:
-
您需要在循环中放入一些调试
print语句。关键问题是您试图同时将words用于两个相互冲突的目的。每一个目的都会破坏对方的进步。使用一个循环变量,另一个来累积结果。 -
@Evorage,你还好吗?
-
哇,谢谢你这么友好
标签: python function for-loop return