invalid literal for int() with base 10: '' for map(int, str(num))
<p>I am running a recursive function that takes in a positive integer and a list (that is updated as it runs through the function):</p>

<pre><code>def happy_number(num, tracking_list = []):
    if (num == 1):
        print 1
    else:
        digit_list = map(int, str(num)) 
        digit_sum = 0
        for n in digit_list:
            digit_sum = (n ** 2) + digit_sum
        if digit_sum in tracking_list:
            print 0
        else:
            tracking_list.append(digit_sum)
            happy_number(digit_sum, tracking_list)
</code></pre>

<p>When running this, it complains about:</p>

<pre><code>digit_list = map(int, str(num)) 
</code></pre>

<p>it gives the error:</p>

<pre><code>ValueError: invalid literal for int() with base 10: ''
</code></pre>

<p>What is happening here?  </p>

<p>It sounds like you are parsing your numbers from a file, and when a space <code>' '</code> or a newline <code>'\n'</code> shows up (or anything that is not a string digit for that matter), your script will fail. You can try:</p>

<pre><code>def happy_number(num, tracking_list = []):
    if (num == 1):
        print 1
    else:
        digit_list = map(int, str(num if num.isdigit() else ''))
        digit_sum = 0
        for n in digit_list:
            digit_sum = (n ** 2) + digit_sum
        if digit_sum in tracking_list:
            print 0
        else:
            tracking_list.append(digit_sum)
            happy_number(digit_sum, tracking_list)
</code></pre>

<p>The line:</p>

<pre><code>digit_list = map(int, str(num if num.isdigit() else ''))
</code></pre>

<p>Will do the same thing as what you had before, except will be an empty list <code>[]</code> if you pass <code>num</code> is not a string digit. Replace the <code>''</code> at the end with another default to if you wish. </p>

python