Once: let's say, after several iterations of WORDS, we have become ["Svitanok"] .
one)
newpos = random.randint(0, length-1)
newpos can be 0, 1, 2, 3 or 4, because length = 5 (no one has changed this variable) and length-1 = 4, and randint includes both bounds in the interval from which the number is selected.
Let's say newpos dropped 4.
2)
newwords.append(WORDS.pop(newpos))
"Svitanok" is 0. There are no elements corresponding to indices 1, 2, 3, or 4. And newpos, we had just 4 - everything, going beyond the right limit of the list, pop drops.
Two: let's go from the very beginning with a different situation.
one)
newpos = random.randint(0, length-1)
Let's say let it fall there 4
2)
newwords.append(WORDS.pop(newpos))
WORDS = ["Svitanok", "hogog", "gfgfgfg", "sdasd"]
newwords = ["adam"]
3)
WORDS.remove(WORDS[newpos])
"Svitanok" is 0, "hogog" is 1, "gfgfgfg" is 2, "sdasd" is 3. And the newpos is 4. That's all, WORDS[newpos] falling, because 4 is going beyond the right limit list.
The correct code is:
import random WORDS = ["Svitanok", "hogog", "gfgfgfg", "sdasd", "adam"] newwords = [] while WORDS: newpos = random.randint(0, len(WORDS) - 1) newwords.append(WORDS.pop(newpos)) print(newwords)