jeudi 11 janvier 2018

For and If Loop together - keeping count

How do I keep a count while executing a IF statement within a for loop? I want to execute different code based on the counter. Here is a simplified version of what I'm trying to do:

li = [1,2,3,4,5]

for l in li:
    counter = 1 
    if counter == 1:
        counter +=1
        print(l)
else:
    print('this worked')

Results:

1
2
3
4
5

Aucun commentaire:

Enregistrer un commentaire