【发布时间】:2020-06-15 12:01:47
【问题描述】:
对于下面的代码,我不会替换旧 txt 中的任何内容。我期待它返回:“我的元音去哪儿了?”
def uncensor(txt, vowels):
c = 0
for a in txt:
if a == '*':
txt.replace('*', vowels[c], 1)
c=c+1
return txt
uncensor("Wh*r* d*d my v*w*ls g*?", "eeioeo")
但是,如果我这样做,我可以替换字母:
txt = 'Wh*r* d*d my v*w*ls g*?'
vowels = 'eeioeo'
txt.replace('*', vowels[0],1)
我哪里做错了?
【问题讨论】:
-
需要使用返回值
txt = txt.replace('*', vowels[c], 1)
标签: python