4.digit_sum.I really don't know what's wrong with my code


I was stucked in this task for a long time.It really confused me.


when I run my code,it tells me that p=p-1 is syntax invalid in line 9.

I hope someone can tell me the reason why there is a syntax error in my code.Thank you very much!

def digit_sum(n):
    if n>0 and n==int(n):
        string = str(n)
        p = len(string)
        count = 0
        while p > 0:
            n = n-(n/(10**(p-1)-1)*(10**(p-1))
            p = p-1
        return summ


The error is before the line that cannot be parsed. p = p - 1 is perfectly valid. The line above is where the problem can be found. Can you guess what it is?


You have to add ) at the end of this line to close the parentheses:

n = n-(n/(10**(p-1)-1)*(10**(p-1))

Your code is still not valid, but it's free of syntax errors. Try to fix other problems without our help :slight_smile:


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