Digital Root without loops Python

后端 未结 6 967
无人及你
无人及你 2021-01-25 07:58
def digit_sum(n):
    \'\'\'(int)->number
    Returns the sum of all the digits in the given integer, n\'\'\'
    if n<10:
        return n
    return n%10 + digit         


        
6条回答
  •  情深已故
    2021-01-25 08:38

    Try this:

    def num(n) :   
         sum = 0   #provided sum as 0
         for i in str(n): 
             a = int(n) % 10    #taken variable a 
             sum  = sum + a   #put the sum.
             n = n/10
         print(sum)    
         if sum < 10:    
             print(str(sum) + "=this is a digital root")
         else:
             num(sum)
    

提交回复
热议问题