4

I am trying to count the number of digits of an input. However, whenever I input 10 or 11 or any two digit number, the output is 325. Why doesn't it work?

inputnumber = int(input())
countnumber = inputnumber
digitcount = 0
while countnumber > 0:
    digitcount += 1
    countnumber = countnumber/10

print(digitcount) 
# result is 325 when input is 10 or 11
smci
  • 32,567
  • 20
  • 113
  • 146
Naya Keto
  • 123
  • 3
  • 8
  • Is it python 2 or 3? – Elisha May 22 '18 at 03:28
  • You can get other ways to do this .. please refer https://stackoverflow.com/questions/2189800/length-of-an-integer-in-python – MD5 May 22 '18 at 03:36
  • @pstatix you don't need to convert to `int` and then to `str`. `input()` is already a string. Just `len(input())` will do. To handle negatives, use `len(str(abs(int(input()))))`. – Austin May 22 '18 at 04:04
  • @Elisha it is Python3. See this question to prove it: https://stackoverflow.com/questions/21316968/division-in-python-2-7-and-3-3 – Ṃųỻịgǻňạcểơửṩ May 22 '18 at 05:22
  • [Because in Python 3 (and 2.7), `countnumber = countnumber/10` will not do integer floor division `//` like you want, it'll do exact (floating-point) division and `countnumber` will become a float](https://stackoverflow.com/questions/1282945/why-does-integer-division-yield-a-float-instead-of-another-integer). Then your iteration will not terminate when you run out of digits, it'll keep going with `countnumber` becoming an ever smaller float every iteration, until it reaches 1e-323 then finally underflows, but of course the digit-count you get (325) is wrong. – smci Aug 15 '23 at 02:30

6 Answers6

5

Your error mainly happened here:

countnumber=countnumber/10

Note that you are intending to do integer division. Single-slash division in Python 3 is always "float" or "real" division, which yields a float value and a decimal part if necessary.

Replace it with double-slash division, which is integer division: countnumber = countnumber // 10. Each time integer division is performed in this case, the rightmost digit is cut.

You also have to watch out if your input is 0. The number 0 is considered to be one digit, not zero.

3

I would not convert that beautiful input to int to be honest.

print(len(input())

would be sufficient.

An easily understandable one liner that no one can complain about.

  • But of course, if negative sign bothers you like wisty said,

    len(str(abs(int (v))))
    

    will be safer for sure.

  • Again, if you are worried about the non numeric inputs like mulliganaceous said, you better cover that case.

    str = input()
    if str.isnumeric():
        print(len(str(abs(v))))
    else:
        print("bad input")
    
Bedir Yilmaz
  • 3,823
  • 5
  • 34
  • 54
1

The reason is that in python 3 the division of two integers yields a floating point number. It can be fixed using the // operator:

number = int(input())
digits_count = 0
while number > 0:
    digits_count += 1
    number = number // 10
Elisha
  • 23,310
  • 6
  • 60
  • 75
1

You must be using Python3, logically your function is right. You just have to change

countnumber = countnumber // 10

because Python3, // is floor division, meanwhile / is true division.

>>>print(1 / 10)
0.1
>>>print(1 // 10)
0

Btw, as @chrisz said above, you can just simply using the len() function to get the number of digits of the input

>>>print(len(input())
Cypherius
  • 521
  • 2
  • 14
  • 3
    `len(input())` does not give right result for a negative number. Also there is no guarantee that input is a number. – Austin May 22 '18 at 03:44
0
num = int(input())
count = 0
while num > 0:
  count += 1
  num = num // 10
print(count)
  • 2
    While this code block may answer the OP's question, this answer would be much more useful if you explain how this code is different from the code in the question, what you've changed, why you've changed it and why that solves the problem without introducing others. – StupidWolf Aug 09 '20 at 09:48
0
def digits(number):
    number = str(number)
    lenght = len(number)
    return lenght

print(digits(25))   # Should print 2
print(digits(144))  # Should print 3
print(digits(1000)) # Should print 4
print(digits(0))    # Should print 1
sv001001
  • 9
  • 1
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 04 '22 at 08:49
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney May 09 '22 at 00:51