Competitive Programming Python: Repeated sum of digits Error
<p>Don't know It's right place to ask this or not. But I was feeling completely stuck. Couldn't find better way to solve it.</p>

<p>I am newbie in competitive Programming. I was solving <code>Repeated sum of digits problem</code> Here is The Question.</p>

<p><code>Given an integer N, recursively sum digits of N until we get a single digit.</code></p>

<p>Example:-</p>

<pre><code>Input:
 2
 123
 9999

Output:-
 6
 9
</code></pre>

<p>Here is my code:-</p>

<pre><code>def sum(inp):
    if type(inp) == int:
        if len(str(inp)) &lt;= 1:
            return inp
        else:
            a = list(str(inp))
            while len(a) &gt; 1:
                b = 0       
                for i in a:
                    b+= int(i)
                a = list(str(b))
    return b

t = int(raw_input())
for i in range(0,t):
    print sum(i)
</code></pre>

<p>While submitting it gave My following error:-</p>

<pre><code>Wrong !! The first test case where your code failed:

Input:
42

Its Correct output is:
6

And Your Output is:
0
</code></pre>

<p>However When I tested my code personally using <code>42</code> It's perfectly gives me correct Output <code>6</code>. </p>

<p>Here is the link of question:- <a href="http://www.practice.geeksforgeeks.org/problem-page.php?pid=1398" rel="nofollow">Repeated sum of digits Error</a></p>

<p>You have not implemented the code properly. You are iterating over <code>i</code> from <code>0</code> to <code>t</code>. Why?? The methodology goes as follows:</p>

<p><code>N = 12345</code></p>

<p>Calculate the sum of digits(1+2+3+4+5 = 15).</p>

<p>Check if it is less than 10. If so, then the current sum is the answer.. If not.. follow the same procedure by setting <code>N = 15</code></p>

<p>Here is the code:</p>

<pre><code>def sum(inp):
    while inp &gt; 9:
        num = inp
        s = 0
        while num &gt; 0:
            s = s + num%10
            num = num/10
        inp = s

    return inp

t = int(raw_input())
for i in range(t):
    n = int(raw_input())
    print sum(n)
</code></pre>

<p><strong>Edit</strong>: I think you are iterating till <code>t</code> because you have considered <code>t</code> to be the number of testcases. So, inside the <code>for</code> loop, you should take another input for <code>N</code> for each of the testcase. [This is based on the <code>input</code> and  <code>output</code> that you have provided in the question]</p>

<p><strong>Edit-2</strong>: I have changed the code a bit. The question asks us to find the repeated sum of <code>t</code> numbers. For that, I have added a loop where we input a number <code>n</code> corresponding to each testcase and find its repeated sum.</p>

python