DML School of Technology
Menu

Getting going, Loops

Add up the digits

The task

Add up the digits of a whole number that is 0 or more. 1234 gives 10.

One way: take the last digit with % 10, then drop it with whole-number division by 10.

Write a function called sum_digits.

Tests

CallExpectedYours
sum_digits(1234) 10 not run
sum_digits(9) 9 not run
sum_digits(1000) 1 not run
sum_digits(0) 0 not run
sum_digits(98765) 35 not run

Next challenge · All challenges

solution.py

Tab indents. To leave the editor, press Escape, then Tab.

Output

Run prints what your code prints. Run the tests to check your function.