Counting number of digits of input using python

前端 未结 5 770
离开以前
离开以前 2021-01-19 18:46

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.

5条回答
  •  终归单人心
    2021-01-19 19:21

    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
    

提交回复
热议问题