0

Im trying to write a function which counts the number of digits in a number - with no string operation. here is my code:

def count_digits(n): count_list=[] while (n>0): n=n%10 i=n count_list.append(i) n=n/10 return len(count_list) n=12345 print count_digits(n) 

By using % i get the last digits - in order to add it to a list. By using / i throw the digit from the number.

The script does not work. For each n i put, the script just prints 1.

Thanks!

5
  • 1
    The first time that n=n%10 is executed, n is reduced to a value between 0 and 9, regardless of its starting value. Commented Dec 28, 2015 at 13:43
  • 3
    return statement is not properly indented. It should be outside the while loop. Commented Dec 28, 2015 at 13:43
  • Also, maintaining the count_list when you could just use an incrementing counter seems kind of silly (only trick is that you handle 0 specially, or use a somewhat kludgy Python equivalent of do/while to avoid the need to handle it specially). Commented Dec 28, 2015 at 13:47
  • 1
    If you import math, you can get the number of digits using int(math.log10(n)) + 1 Commented Dec 28, 2015 at 14:02
  • @eumiro: Cheater. ;-) (Note: I up-voted, but I suspect it would still count as cheating; this sounds like homework) Commented Dec 28, 2015 at 14:04

3 Answers 3

3

There are several problems to your code:

  • The return statement should be outside of the loop.
  • The n = n % 10 statement modifies n, making it impossible to get the other digits.
  • I'd use the integer division operator //. In Python 3, n / 10 would give a floating point number.
  • Like ShadowRanger said, the current solution considers 0 as having 0 digits. You need to check whether n is 0.

Here is a corrected version of your code:

def count_digits(n): if n == 0: return 1 count_list = [] while n > 0: count_list.append(n % 10) n = n // 10 return len(count_list) 

Also, as it was said in the comments, since your goal is just to count the digits, you don't need to maintain a list:

def count_digits(n): if n == 0: return 1 count = 0 while n > 0: n = n // 10 count += 1 return count 
Sign up to request clarification or add additional context in comments.

2 Comments

I'd also say that using a list is totally unnecessary, even if it is an acceptable solution
Note: This code (and the original) will treat 0 as having zero digits, when usually, it should still count as having one digit. It can be handled by either making the while condition while True with an if n <= 0: break at the end of the loop (so 0 gets counted once), or by adding an if n == 0: return 1 to the top of the function. Obviously, still doesn't handle negative numbers (but handling them involves defining what digit count means; simplest would be to assign n = abs(n) at the top of the function and ignore the sign).
2

Perhaps you can try this, a much simpler approach. No lists involved :)

def dcount(num): count = 0 if num == 0: return 1 while (num != 0): num /= 10 count += 1 return count 

Comments

1

count_list stores the digits.

def count_digits(n): count_list=[] while (n>0): count_list.append(n%10) n-=n%10 n = n/10 return count_list n=12345 print len(count_digits(n)) 

Without using a list

def count_digits(n): count_list=0 while (n>0): count_list+=1 n-=n%10 n = n/10 return count_list n=12345 print count_digits(n) 

6 Comments

Why do n -= n % 10? It makes count_list as list of 0s, which doesn't really matter (you just get the len of it anyway), but it costs more work for zero gain.
@ShadowRanger Thanks
Still unnecessary. Switch to n //= 10 (or n = n // 10 if you prefer) and in both Py2 and Py3 it will do floor division; subtracting the remainder becomes unnecessary (and you remove the opportunity for floating point errors to bite you).
@ShadowRanger Yes, It is eskaev solution, thank you for your comment.
Its working now but im not sure i understand WHY. I cant find the difference in the codes. I see that you changed it (to ' n- = n%10') but i cant understand the difference; if for example n=123, then n-=120. So we add an object (120) to the list. OK. But we just put Different object with keeping the same number of objects, no ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.