 <p>I got the solution. The reason why <code>y</code> didn't increase was because the value of <code>x</code> didn't get reset from <code>1000</code>. It just automatically skipped that chunk of code because the value of <code>x</code> was already 1000. This is my improved code which also sorts the array in order. </p> <pre class="lang-py prettyprint-override"><code>def Problem4(): y = 100 a = [] x1 = [] y1 = [] while y &lt; 1000: y = y + 1 x = 100 while x &lt; 1000: z = x*y if str(z) == str(z)[::-1]: a.append(z) x = x + 1 a.sort() print(a) Problem4() </code></pre>