i’m trying to make a loop, and every time it loops it adds one to a variable. instead, it adds one as soon as the loop starts and never again.
i tried this:
for x in sweden:
cnote = 0
print(sweden.notes[cnote])
cnote += 1
print(cnote)
it’s meant to add one to cnote everytime it loops. issue is, it starts on one somehow and never adds again. anyone have an idea on how to fix this? i’m stumped, i’ve tried using cnote = cnote + 1, +=, and i can’t think of anything else.
>Solution :
The issue you’re facing is that ‘cnote’ is defined within the loop not outside it. Therefore, whenever it loops it will reset back to 0 and then add a 1. You should make the code as such:
cnote = 0
for x in sweden:
print(sweden.notes[cnote])
cnote += 1
print(cnote)