增加循环内的变量

时间:2018-01-14 14:28:18

标签: python

我想跟踪执行循环的次数。并且,当它执行超过x次时,执行某些操作。下面的代码不起作用;变量a始终为0

bup = set(line.strip() for line in open('C://Users/'+username+'/Desktop/manifest.backup'))
orig = set(line.strip() for line in open(direc.replace('/something','')+'/manifest.json'))

for line in bup & orig:
    if line:
        if line[0] is '"':
            a = 0
            if line[1] is 'b':
                print('strings that start with b match!')
                break
            else:
                print(len(set(bup) & set(orig)))
                print(line)
                print(a)
                a = a + 1
                if a >= (len(set(bup) & set(orig))-2):
                    print('Conclusion')

1 个答案:

答案 0 :(得分:0)

那是因为你在每个循环开始时继续将a重置为0。将行a = 0移到循环之外(for之前)。