2

I have written this code in editor:

#!/usr/bin/env python3


def digits(n):
    count = 0
    if n == 0:
      count = 1
    while n > 0:
      n /= 10
      count += 1
    return count

print(digits(25))   # Should print 2
print(digits(144))  # Should print 3
print(digits(1000)) # Should print 4
print(digits(0))    # Should print 1

But I am getting like:

325
326
327
1

Is this wrong logic or, Am i missng somthing in this?

Mritunjay
  • 27
  • 1
  • 7

1 Answers1

4

/= performs floating-point division. Use //= for integer division.

def digits(n):
    count = 0
    if n == 0:
        count = 1
    while n > 0:
        n //= 10
        count += 1
    return count
John Kugelman
  • 349,597
  • 67
  • 533
  • 578