我不能 return 一个值
I can't return a value
我正在尝试编写一个函数,将输入转换为所谓的 "cow Latin." 我想 return 来自 if
语句的值,但每当我这样做时得到一个语法错误。我可以打印该值,但我也想避免 returning None
函数。
def cow_latinify_sentence(sentence):
vowels = tuple('aeiou1234567890!@#$%^&*()-_=+|\][}{?/.\',><`~"')
sentence = sentence.lower()
sentence_list = sentence.split()
for i in range(len(sentence_list)):
cow_word = sentence_list[i][:]
if cow_word.startswith(vowels):
print('{0}moo'.format(cow_word), end=' ')
else:
cow_word = sentence_list[i][1:] + sentence_list[i][:1]
print('{0}oo'.format(cow_word), end=' ')
cow_latin = cow_latinify_sentence("the quick red fox")
print(cow_latin)
简而言之,我怎样才能让函数变成 return
而不是 print
?
为什么不直接替换
的两个实例
print('{0}moo'.format(cow_word), end=' ')
和
return '{0}moo'.format(cow_word)+' '
你必须摆脱end=
;您不必替换 print
输出后的换行符,但如果您想要在返回字符串的末尾添加 space,您仍然必须自己附加它。
def cow_latinify_sentence(sentence):
vowels = tuple('aeiou1234567890!@#$%^&*()-_=+|\][}{?/.\',><`~"')
sentence = sentence.lower()
sentence_list = sentence.split()
result = ''
for i in range(len(sentence_list)):
cow_word = sentence_list[i][:]
if cow_word.startswith(vowels):
result += ('{0}moo'.format(cow_word) + ' ')
else:
result += '{0}oo'.format(sentence_list[i][1:] + sentence_list[i][:1]) + ' '
return result.strip()
>>> cow_latinify_sentence('hello there i am a fish')
'ellohoo heretoo imoo ammoo amoo ishfoo'
您需要创建一个列表来累积您的结果。
result = []
您函数中的两个打印语句需要更改为 result.append(XXXX)。然后当你处理完整个句子后你可以
return (result)
或者,重新组成一个句子:
return " ".join(result) + '.'
def cow_latinify_sentence(sentence):
vowels = tuple('aeiou1234567890!@#$%^&*()-_=+|\][}{?/.\',><`~"')
sentence = sentence.lower()
sentence_list = sentence.split()
result = ''
for i in range(len(sentence_list)):
cow_word = sentence_list[i][:]
if cow_word.startswith(vowels):
result += '{0}moo'.format(cow_word) + ' '
else:
result += '{0}oo'.format(sentence_list[i][1:] + sentence_list[i][:1]) + ' '
return result.strip()
我正在尝试编写一个函数,将输入转换为所谓的 "cow Latin." 我想 return 来自 if
语句的值,但每当我这样做时得到一个语法错误。我可以打印该值,但我也想避免 returning None
函数。
def cow_latinify_sentence(sentence):
vowels = tuple('aeiou1234567890!@#$%^&*()-_=+|\][}{?/.\',><`~"')
sentence = sentence.lower()
sentence_list = sentence.split()
for i in range(len(sentence_list)):
cow_word = sentence_list[i][:]
if cow_word.startswith(vowels):
print('{0}moo'.format(cow_word), end=' ')
else:
cow_word = sentence_list[i][1:] + sentence_list[i][:1]
print('{0}oo'.format(cow_word), end=' ')
cow_latin = cow_latinify_sentence("the quick red fox")
print(cow_latin)
简而言之,我怎样才能让函数变成 return
而不是 print
?
为什么不直接替换
的两个实例print('{0}moo'.format(cow_word), end=' ')
和
return '{0}moo'.format(cow_word)+' '
你必须摆脱end=
;您不必替换 print
输出后的换行符,但如果您想要在返回字符串的末尾添加 space,您仍然必须自己附加它。
def cow_latinify_sentence(sentence):
vowels = tuple('aeiou1234567890!@#$%^&*()-_=+|\][}{?/.\',><`~"')
sentence = sentence.lower()
sentence_list = sentence.split()
result = ''
for i in range(len(sentence_list)):
cow_word = sentence_list[i][:]
if cow_word.startswith(vowels):
result += ('{0}moo'.format(cow_word) + ' ')
else:
result += '{0}oo'.format(sentence_list[i][1:] + sentence_list[i][:1]) + ' '
return result.strip()
>>> cow_latinify_sentence('hello there i am a fish')
'ellohoo heretoo imoo ammoo amoo ishfoo'
您需要创建一个列表来累积您的结果。
result = []
您函数中的两个打印语句需要更改为 result.append(XXXX)。然后当你处理完整个句子后你可以
return (result)
或者,重新组成一个句子:
return " ".join(result) + '.'
def cow_latinify_sentence(sentence):
vowels = tuple('aeiou1234567890!@#$%^&*()-_=+|\][}{?/.\',><`~"')
sentence = sentence.lower()
sentence_list = sentence.split()
result = ''
for i in range(len(sentence_list)):
cow_word = sentence_list[i][:]
if cow_word.startswith(vowels):
result += '{0}moo'.format(cow_word) + ' '
else:
result += '{0}oo'.format(sentence_list[i][1:] + sentence_list[i][:1]) + ' '
return result.strip()