| 
 | 
 
 
 楼主 |
发表于 2021-9-17 10:50:39
|
显示全部楼层
 
 
 
 本帖最后由 全桥整流 于 2021-9-17 10:54 编辑  
 
我的代码 
- t = input('write a name: ')
 
 - s = input('write you want to remove word: ')
 
 - y = input('write you want word: ')
 
 - doc=open(t,'r+')
 
 - f = doc.read()
 
 - k = f.count(s)
 
 - print('have %s in %s'%(k,t))
 
 - r =input('do you want to remove?(y/n): ')
 
 - if r =='y': 
 
 -     f = f.replace(s,y)
 
 - doc.write(f)
 
 - doc.close()
 
 -     
 
 -     
 
  
-     
 
 -     
 
 
  复制代码 
 
你的代码 
t = input('write a name: ') 
s = input('write you want to remove word: ') 
y = input('write you want word: ') 
doc=open(t,'r+') 
f = doc.read() 
k = f.count(s) 
print('have %s in %s'%(k,t)) 
r =input('do you want to remove?(y/n): ') 
if r =='y': 
    f = f.replace(s,y) 
doc.close() 
doc = open(t,'w') 
doc.write(f) 
doc.close() 
 
 
是我少写了点东西,不好意思。 
但是为什么我的代码会只替换一部分? |   
 
 
 
 |