dimanche 26 août 2018

how to find the sum of digits of sum of digits of each of T numbers repeatedly in python till it gets to being a single digit number?

Here is the code that i wrote to get sum of digits from a number repeated number of times till the sum gets below 10:

T = int(input())
for i in range(T):
    N = int(input())
    def P():
            M = [int(d) for d in str(N)]
            N = sum(M)
            if N<10:
                    print(N)
            else :
                    return P()
    P()

On running this code it gives me an error like:

 Traceback (most recent call last): 
 File"C:/Users/AdityaShrivastava/AppData/Roaming/Python/Python36/Scripts/tes 
 ting.py", line 11, in <module>
 P()
 File "C:/Users/Aditya 
 Shrivastava/AppData/Roaming/Python/Python36/Scripts/testing.py", line 5, in 
 P
 M = [int(d) for d in str(N)]
 UnboundLocalError: local variable 'N' referenced before assignment

Aucun commentaire:

Enregistrer un commentaire