Why this not work?


  • 0
    S
    enter code here
    def addDigits(num):
     total=0
     while (len(list(str(num)))!=1):
       n=list(str(num))
       for num1 in n:
          total=total+int(num1)
       num=total
     return num

  • 1
    T

    You should put your put your total=0 inside the loop, like this:

     while (len(list(str(num)))!=1):
       total=0
       n=list(str(num))
       for num1 in n:
          total=total+int(num1)
       num=total
       
     return num

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.