Python script for finding keith numbers not working
<p>I am trying to make a python program that will find keith numbers. If you don't what keith numbers are, here is a link explaining them:
<a href="http://mathworld.wolfram.com/KeithNumber.html" rel="nofollow">Keith Numbers - Wolfram MathWorld</a></p>

<p>My code is</p>

<pre><code>from decimal import Decimal
from time import sleep

activator1 = 1

while (activator1 == 1):
    try:
        limit = int(raw_input("How many digits do you want me to stop at?"))
        activator1 = 0
    except ValueError:
        print "You did not enter an integer"


limitlist = []
activator2 = 1

while (activator2 &lt;= limit):
    limitlist.append(activator2)
    activator2 += 1
    print limitlist

add1 = 0
add = 0
count = 9
while 1:
    sleep (0.1)


    numbers = list(str(count))

    for i in limitlist:
        if (i &gt; 0) &amp; (add &lt; count):
            add = sum(Decimal(i) for i in numbers)
            lastnumber = int(numbers[-1])
            add1 = lastnumber+int(add)
            numbers.reverse()
            numbers.pop()
            numbers.append(add1)
            print add1
            print add
            print count
            print numbers
        if (add1 == count):
            print"________________________________"
            print add1
            print count

        elif (i &gt; 0) &amp; (add &gt; count):
            count += 1
            break
</code></pre>

<p>It doesn't output any errors but it just outputs </p>

<pre><code>18
9
9
[18]
</code></pre>

<p>Could someone please tell me why it doesn't just repeatedly find Keith numbers within the number of integers range? </p>

<p>You have it in front of you:</p>

<pre><code>add1  = 18
add   = 9
count = 9
numbers = [18]
</code></pre>

<p>You're in an infinite loop with no output.  You get this for <em>once</em>.  After this, <strong>i</strong> runs through the values 1, 2, and 3.  Each time through the <strong>for</strong> loop, all three <strong>if</strong> conditions are <strong>False</strong>.  Nothing changes, you drop out of the <strong>for</strong> loop, and go back to the top of the <strong>while</strong>.  Here, you set <strong>numbers</strong> back to ['9'], and loop forever.</p>

<p>I suggest that you learn two skills:</p>

<ol>
<li>Basic debugging: learn to single-step through a debugger, looking at variable values.  Alternately, learn to trace your logic on paper and stick in meaningful <strong>print</strong> statements.  (My version of this is at the bottom of this answer.)</li>
<li>Incremental programming:  Write a few lines of code and get them working.  <em>After</em> you have them <em>working</em> (test with various input values and results printed), continue to write a few more.  In this case, you wrote a large block of code, and then could not see the error in roughly 50 lines.  If you code incrementally, you'll often be able to isolate the problem to your most recent 3-5 lines.</li>
</ol>

<hr>

<pre><code>while True:
    # sleep (0.1)

    numbers = list(str(count))
    print "Top of while; numbers=", numbers

    for i in limitlist:
        print "Top of for; i =", i, "\tadd =", add, "\tcount =", count, "\tadll =", add1
        if (i &gt; 0) &amp; (add &lt; count):
            add = sum(Decimal(i) for i in numbers)
            lastnumber = int(numbers[-1])
            add1 = lastnumber+int(add)
            numbers.reverse()
            numbers.pop()
            numbers.append(add1)
            print "add1\t", add1
            print "add\t", add
            print "count\t", count
            print "numbers", numbers
        if (add1 == count):
            print"________________________________"
            print add1
            print count

        elif (i &gt; 0) &amp; (add &gt; count):
            count += 1
            print "increment count:", count
            break
</code></pre>

python python-2.7 math numbers