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


#1



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

https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/4?curriculum_id=4f89dab3d788890003000096#


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:
            count+=n/10**(p-1)-1
            n = n-(n/(10**(p-1)-1)*(10**(p-1))
            p = p-1
        summ=count
        return summ


#2

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?


#3

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:


#4

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