Форум сайта python.su
Условие:
In this kata, you must create a digital root function.
A digital root is the recursive sum of all the digits in a number. Given n, take the sum of the digits of n. If that value has two digits, continue reducing in this way until a single-digit number is produced. This is only applicable to the natural numbers.
Мой код:
def digital_root(n=1238142342342): while n > 10: b = 0 c = str(n) for i in range(len(c)): a = n % 10 b += a n = n//10 n = b print(n) digital_root()
Офлайн
У тебя return нет в функции. Когда return нет, функция выполняет return None.
Отредактировано py.user.next (Авг. 12, 2018 00:06:36)
Офлайн
def digital_root(n): return n if n < 10 else digital_root(sum(map(int, str(n))))
Офлайн