我刚刚开始学习python,我想知道它怎么了 这是for循环和/或if语句。你知道吗
我试着循环一系列单词,如果我发现 一个词“right”我想用“left”来代替它。你知道吗
def left_join(phrases):
"""
Join strings and replace "right" to "left"
"""
mlist = list(phrases)
for word in mlist:
if word == "right":
word = "left"
output = ','.join(mlist)
return output
phrases = ("left", "right", "left", "stop")
left_join(phrases)
我应该得到这个
"left,left,left,stop"
但我明白了
"left,right,left,stop"
所以“右”不能用“左”代替。为什么?你知道吗
请尝试以下代码:
引用official documentation on for loop
所以
mlist
没有改变,这里只改变了word
相关问题 更多 >
编程相关推荐