A function may call itself
The digits of 4729 add up to 9 plus the digits of 472, and 472 is the same kind of question, just smaller. Like Russian nesting dolls, each doll contains a smaller copy of itself. So the answer for the whole number is the last digit plus the answer for the rest.
A function that calls itself like this is recursive. Trust the smaller call to return the right answer, and add your part.
sum_digits(4729): keep the last digit 9, and ask sum_digits(472).
Every recursion needs a base case
If the function always called itself, it would never stop, like a dream inside a dream inside a dream. So pick a case small enough to answer directly: a number with one digit is its own digit sum.
Check it first, before the smaller call. Each call must also move closer to the base case, or the calls never end.
def sum_digits(n):
if n < 10:
return n
return n % 10 + sum_digits(n // 10)