You are mixing recursion and looping in a bit of an odd way. The problem is while True:. Because you never return anything from within the loop there is nothing stopping it from going on forever. Your code reaches 1 then just keeps adding to the length. Here is a fixed version.