Why does it say I have an infinite loop?


#1

Question

Why does it say I have an infinite loop?

Answer

If you wrote something along the lines of start_list.append(...) inside of your for loop, then you have created an infinite loop.
Think about it like this:
Our loop goes over each item in start_list
It enters the loop and appends a number to start_list
Loop repeats as next number in start_list and then appends another number to that same list
Do you see the pattern here? That method infinitely appends items to the list and can never possibly exit the loop, thus the infinite loop error message!
To fix this, be sure to .append() items to the square_list, which exists solely to hold your squared values!
Also, be sure to .sort() your list after the loop, not inside of it. Sorting it each time the loop runs is a costly operation, we only need to sort it once after everything has been appended. And make sure you are sorting the square_list.


FAQ: Learn Python: Python Lists and Dictionaries - More with 'for'