我有 10 个文件,其中包含 100 个名为 randomnumbers(1-10).py 的随机数。我想创建一个程序,当找到一个字符串 123 时说“恭喜”,并计算 123 出现的次数。我有“祝贺”部分,我已经为计数部分编写了代码,但结果总是为零。怎么了?
for j in range(0,10):
n = './randomnumbers' + str(j) + '.py'
s='congradulations'
z='123'
def replacemachine(n, z, s):
file = open(n, 'r')
text=file.read()
file.close()
file = open(n, 'w')
file.write(text.replace(z, s))
file.close()
print "complete"
replacemachine(n, z, s)
count = 0
if 'z' in n:
count = count + 1
else:
pass
print count