Kodewick
Start learning →
Basic←→0 / 100

Add up the digits

#085BasicNumbersLoops4 min

In a card game, a card's power is the sum of the digits of its number. Write digit_sum(n): card 123 has power 1 + 2 + 3 = 6. n is 0 or a positive whole number.

Examples
digit_sum(123)→6
digit_sum(909)→18
main.py
def digit_sum(n):
    total = 0
    for ch in str(n):
        # your code here: turn this digit back into a number, add it to total
        pass
    return total
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.