- why does it work the first time?
because for letters in letters: initialise the iteration on the name letters (which is a list of strings). Even if the letter variable is reassigned, the original list is already stored in the for iterator, so the loop runs properly
- why it doesn't work / works so weirdly the next times?
After the first iteration, the name letters is no longer referencing your start list, but is referencing the last element of the original list, AKA c. Since strings are iterable, you don't get an error (like you would have gotten if letters were a list of integers, for instance), it just yields c over and over.
note that for letters in letters in itself makes no sense. The list of letters is properly named letters, but the variable name should be named letter, for instance, else it makes the code unclear.
letters = 'a',letters = 'b',letters = 'c'the first time. The next time, it’s just looping over'c'.for letters in lettersis the culprit. change your loop variable name, specially in an inner loop (or resetlettersinside the outer loop). Anyway, it should befor letter in letters.letters == 'c'. So on the second iterationfor letters in letters:you are iterating over the string"c'