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
intand then tostr.input()is already a string. Justlen(input())will do. To handle negatives, uselen(str(abs(int(input())))).countnumber = countnumber/10will not do integer floor division//like you want, it'll do exact (floating-point) division andcountnumberwill become a float. Then your iteration will not terminate when you run out of digits, it'll keep going withcountnumberbecoming 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.