我希望将字符串中的大小写的‘o’,‘t’, ‘n’转换成 ‘’,‘-’, ‘+’
这里是我写的函数
def ecry(word):
for i in range(len(word)):
n_wds=words.replace('o','')
n_wds=words.replace('O','*')
n_wds=words.replace('t','-')
n_wds=words.replace('T','-')
n_wds=words.replace('n','+')
n_wds=words.replace('N','+')
return n_wds
但是好像没什么用
第一,不需要for 循环
第二,你函数参数是word, 下边使用的却是words
第三,第二次.replace()替换时要用上一次替换的结果n_wds再进行替换,
不能是都用原始word字符串进行替换,那样只能保留最后一个.replace()替换的结果
代码修改如下:
def ecry(word):
n_wds=word.replace('o','*')
n_wds=n_wds.replace('O','*')
n_wds=n_wds.replace('t','-')
n_wds=n_wds.replace('T','-')
n_wds=n_wds.replace('n','+')
n_wds=n_wds.replace('N','+')
return n_wds
print(ecry('oaTbn'))
如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!