Start free

Intermediate · Functions · 114 of 264

Digital root

Add up the digits of a number, then the digits of the result, and so on until one digit is left. Print that digit.

Input: a positive whole number. Output: its digital root.

Examples

Input
942
Output
6
Input
5
Output
5
Input
99999999999
Output
9

One more test checks other inputs when you press Check.

Stuck? The idea behind functions

A function, made with def, gives a name to a piece of code, so it can be used again: its parameters are the values it is given, return hands an answer back.

A function that returns lets the caller decide what to do with the answer: print it, compare it, use it in a sum.

def is_even(n):
    return n % 2 == 0

print(is_even(4))
print(is_even(7))

Watch for: print inside a function is not return: the caller gets None. And the code after return in the same block never runs.

All the functions exercises
Your code
Opening the code editor…
Python 3.14 in your browser
Output
Run shows what your program prints. Check tests it on every example and the hidden tests.

More functions exercises