4. digit_sum


def digit_sum(n):
return x+y+z

This is the code I wrote using the modulo method.. but it doesn't take into account numbers greater than 999.
How to make it a generalized code?

4. digit_sum Modulo Help

You should be using a for-loop to repeat the process for the number of digits in the number.
Then you would want to obtain the sum by adding it to a variable for that same number of times.

Hope this helps!


I am not against using for-loop. But in my opinion, for this example, using while loop will be way easier to code and to read for everyone else.

  1. initialize a variable to hold the sum before while loop.
  2. Think of the number (very popular number, remember i'm saying "the number") when you would want while loop to stop.
    #Inside while loop
    -number modulus by 10
    -sum adds to itself and the digit
    -divide the original number by 10 and store it to itself. (This part could be little trickier)
  3. Return sum

Hope this helps :slight_smile:



I'm attempting the same question (sorry if piggy-backing isn't allowed, it's my first post!)

I got to this point, but it's not working. Argh! It seems to only go through the while loop once instead of as many times as the length of the number. Ie for 434 it returns 4 instead of 11, for 123 it returns 3 instead of 6 etc.
Any advice?


never mind - got it - just had the indentation wrong for my 'return'


def digit_sum(n):
result = 0
while n >= 10:
lastDigit = n % 10
print "The current last digit is " + str(lastDigit)
result += lastDigit
n = n // 10
print "The next number will be " + str(n)
result += n
return result

print digit_sum(45367343241)

works on any positive integer


This works for me with a for-loop

def digit_sum(n):
    result = 0
    m = str(n)
    for i in range(len(m)):
        lastDigit = n % 10
        print 'Current last digit: ' + str(lastDigit)
        result += lastDigit
        n = n // 10
    return result

print digit_sum(123456)


Here's mine, hope this helps someone.

def digit_sum(n):
    num_list = [] #make empty list
    for number in str(n): #turn it into string
        number = int(number) #then back into number
        num_list.append(number) #add all of them to the empty list
    n = sum(num_list) #sum up all numbers in list
    return n #return the sum

digit_sum(434) #use any number


Here's how I did it - short and does the job, and has a very readable print out:

def digit_sum(n):
    total = 0
    print "The Math:"
    for digit in str(n):
        print "%s + %s = %s" % (total, digit, total + int(digit))
        total += int(digit)
    print "__________"
    print "Total = %s" % total
    return total

What this prints out:

The Math:

0 + 1 = 1
1 + 3 = 4
4 + 3 = 7
7 + 7 = 14

Total = 14

What it looks like with no printing:

def digit_sum(n):
    total = 0
    for digit in str(n):
        total += int(digit)
    return total


i did the same what you did and it worked but i edited some variables now please explain it for me because i can't understand it


m = str(n)  # convert the number to string, so "for loop" can iterate 
for i in range(len(m))  # for loop iterates till the end of the previously converted number
lastDigit = n % 10  # if you use modulo on a number it gives you the last number. e.g: 1234 % 10 = 4
result += lastDigit  # add the last number to the result
n = n // 10  # cut the last number. e.g: 1234 // 10 = 123

I hope this is clear :slight_smile:


Hi folks,

This is my paltry attempt; it seems to get stuck on the first number. Can someone explain why it's not working?

def digit_sum(n):
total = 0
control = len(str(n))
print control
for number in str(n):
if control > 0:
total += int(number)
print total
control -= 1
print control
print total
return total




nevermind, return was indented in the for loop, when it needed to be back one. small fix :slight_smile:


Hello guys,
It looks like my code meets the requirements, the codeacademy does not think so though. What is wrong? It keeps saying Oops, try again. Your function fails on digit_sum(434). It returns None when it should return 11. Although I can obtain 11 with my written code.
Here is my code:
def digit_sum(n):
for l in str(n):

print summa



Sorry, found the problem. I have forgotten to return the output


def digit_sum(n):
while n>0:
return x


def digit_sum(n):
    number_list = []
    for c in str(n):
    return sum(number_list)
print digit_sum(434)


this makes the most sense for me. Thank you!


def digit_sum(n):
ds = str(abs(n))
s = 0
for i in range (0, len(ds)):
s += int(ds[i])
return s


thanks for posting this is almost exactly how I wrote mine. can you remind me why the i is in brackets?