Digit_sum - what's wrong with my loop


#1

I keep getting the following error message "The program took too long to finish. Check your code for infinite loops
and try again". I thought that my loop was not infinite because n will eventually hit zero. Where did I go wrong?

def digit_sum(n):
    n_sum = 0
    while n != 0:
        n_sum = n_sum + (n % 10)
        n // 10
    return n_sum


#2

you never update n, as we can demonstrate as such:

n = 100
print n // 10 # 10
print n # 100
n = n // n
print n # 10

#3

Oh I see my mistake

Thanks :slight_smile:


#4