Skip to content

Loops · routine

Count the digits

Write count_digits(n) for a whole number n >= 0, returning how many digits it has — without turning it into a string.

n // 10 drops the last digit. count_digits(0) is 1.

Run adds print(count_digits(2024)) after your code, to try it.

Stuck on the idea rather than the code? The Doing it again lesson walks through it.