[Solved] My Python for loop executes 5005 times but it is meant to execute 100 times [closed]


Let’s simplify your program to understand how to get 5005 results:

gencodelist = []
for i in range(1, 100):
  gencodelist.append(str(i) + ' ')
  gencodeout="".join(gencodelist)
  print(gencodeout)

Here we are adding elements to the list gencodelist 99 times (from 1 to 99), and print all elements of this list each time. So, we have 1 + 2 + ... + 99 numbers in the output (it fact it is 4950, not your number 5005).

So, the problem is simple: you do not clear gencodelist in the very beginning of the function gencode().

1

solved My Python for loop executes 5005 times but it is meant to execute 100 times [closed]