We all are so clever!


We all are so clever!)))

Look at my ■■■■-code.

def digit_sum(n):
    if n<0:
        return False
        while total>0:
            print a
            total -=1
            print b
            return b

Digit_sum need help with it all

But this doesn't work.Throws a "local variable "total" referenced before assignment" error.


both this:

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

and this:

total = 0
for x in str(n):
    total += int(x)
return total

work fine, if you have a problem with the exercise, please make a new topic so someone can help you


Yes the first one does work fine, should have clarified. The second, however, does not.


yea, because the function is missing, the second one is only the function content.


I get that but even then it throws the error. I ended up creating the list instead so I solved the actual problem, just wanted to point out that that doesn't seem to work for me.


works perfectly fine:

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


You have the right idea but the application is a little off.

total = 0
while n > 0:
    total += n % 10
    n //= 10
return total


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