4. Digit_sum


#1



4. Digit Sum


I've managed to (accidentally) work out the first method of converting x into a string, splitting it and then turning it back into an integer to add together, but can anyone take me through how to phrase the method using the modulo and floor divide functions?

So far I have this:

def second_go(x):
    result = 0
    while x > 0:
        m = x % 10
        result += m
        x // 10
    return result


I'm just creating an infinite loop with my while statement. Any suggestions? Or if anyone can post a working method using modulo and floor divide, that'd be really useful. Thanks in advance.


#5

Change

x // 10

to

x = x // 10

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.