Coding basics · 11 of 20
Count the digits
Return how many digits a non-negative integer n has.
What you need
// divides and drops the fraction, so n // 10 removes the last digit. A while loop repeats as long as its condition is true.
n = 472
n = n // 10 # 47
while n > 0:
n //= 10 # 4, then 0Examples
Input n = 12345
Output 5
Input n = 0
Output 1
Zero is written with one digit.