Уведомления

Группа в Telegram: @pythonsu

#1 Авг. 11, 2018 22:25:57

Bopo6eu
Зарегистрирован: 2018-08-11
Сообщения: 1
Репутация: +  0  -
Профиль   Отправить e-mail  

Задача с CODEWARS

Условие:

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()

Test Results:
None should equal 7
None should equal 6
None should equal 2
None should equal 9
None should equal 9
None should equal 0


Что он от меня хочет???

Офлайн

#2 Авг. 12, 2018 00:06:24

py.user.next
От:
Зарегистрирован: 2010-04-29
Сообщения: 9716
Репутация: +  842  -
Профиль   Отправить e-mail  

Задача с CODEWARS

У тебя return нет в функции. Когда return нет, функция выполняет return None.



Отредактировано py.user.next (Авг. 12, 2018 00:06:36)

Офлайн

#3 Авг. 28, 2018 12:28:04

Vigi
От: Курья, Алтай
Зарегистрирован: 2015-02-07
Сообщения: 144
Репутация: +  8  -
Профиль   Отправить e-mail  

Задача с CODEWARS

 def digital_root(n):
    return n if n < 10 else digital_root(sum(map(int, str(n))))

Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version