4/15 digit_sum

def digit_sum(n):
    for s in range(len(str(n))):
    return a

what wrong in my code... it prompts that sum of 434 is 12 instead of 11...??


I don't quite understand your code but i simply stated

n = str(n)

and then used for loop like

for i in n:
x += int(n)


def digit_sum(n):
if n < 10 :
return n
return n % 10 + digit_sum( n // 10 )
you can try the above code pattern
i used recursion though


def digit_sum(n):

    for s in range(len(k)) :

    return a

yeh .. i finally achieved the correct one using for loop.. this makes more sense...

it took 3 days for me to figure it .. now im happy that this code finally works...cheers...

hoonster83.. i too didn't understand my previous code


For me this code worked..

def digit_sum (n):
    total = 0
    num = len(str(n))
    num1 = 10**(num-1)
    for i in range(num):
        total = n/num1 + total
        n = n-(n/num1)*num1
        num1 = num1/10
    return total
print digit_sum (1234)

though its little complicated..will try to simplify it.. :smile:

def digit_sum(n):
    working_total = []
    n = str(n) #n is now a string
    for x in n:
        x = int(x) #every digit should now be an independent int
    return sum(working_total)

I think the thing they want you to remember here is that there is a connection between ints and strings in that you can convert an int to a string to pick it apart then just change it back to do whatever math you need to.


I've tried using for and while loops, including the syntax that mvsenthilk posted but it doesn't work for me and I don't know why. Can someone post a complete function with a loop so I can see where I went wrong? Thanks!


Have you learned about list comprehensions, yet? This is one of the simplest approaches but a little bit abstract. We can deconstruct it, afterwards:

def digit_sum(n):
    return sum([int(i) for i in str(n)])

If we study the example in the previous post, we can see all the working parts in this expression.

  1. convert n to a string object`
  2. iterate over the object
  3. convert each character to an integer
  4. append it to the list
  5. return the sum of the digits in the list



If you change that list comp. into a straight generator you would save some issues on larger data sets. On small data sets its fine but anything too large and you would see an improvement because it won't create a temporary list in memory.

Also another thing of note is that both generators and list comprehension are converted into bytecode by python making it SUPER quick to run. It can run faster than comparable for loops quite easily.

Python Code: @mtf Code as a generator

def digit_sum(n):
    return sum((int(i) for i in str(n)))

As you can see the generator is identical to the list comprehension minus the leading and trailing brackets. This is because list comprehension is just a generator with a special purpose to populate a list with the generator automagically.


Ah, yes, the generator. Keep meaning to get more practice with those. They do make a lot of sense and are easy on resources, as you say. Thanks for pointing this out.