for while改變index

CV計算機視覺工程師發表於2020-11-18
# 此處為了改變迴圈值,index按照指定變化
# for迴圈和while迴圈對比
a = [1,0,0,1,1,1,0,1]
count = 0
num = 0
while num < len(a):
    if a[num] == 1:
        num = num + 3
        count = count + 1
    else:
        num =num +1
print(count)
# for
count = 0
num = 0
for num in range(len(a)):
    print(num)
    if a[num] == 1:
        num = num + 3
        count = count + 1
    else:
        num =num +1
print(count)

 

相關文章