n=int(input())
count=0
for i in range(n):
n=n//10
count=count+1
print(count)
What is wrong in my code?
n=int(input())
count=0
for i in range(n):
n=n//10
count=count+1
print(count)
What is wrong in my code?
Hello , I do not know python as I code in C++ , but I can figure out something that
this for loop should stop when the value of n is 0
for i in range(n):
``
I guess you cannot use for loop here
so instead use while
n=int(input("Enter number:"))
count=0
while(n>0):
count=count+1
n=n//10
print("The number of digits in the number are:",count)
Basically the issue is that the for
loop is not the tool for the job; if you have an input value of 999, for example, you don’t really want to loop 999 times, which is what range(n)
will give you; it won’t get updated by subsequently changing the value of n
.
The suggestion of a while
loop is better. If you want to catch the n==0
case in the same code, start the count at 1
with a division:
n = abs(int(input()))
dgts = 1
red = n//10
while red > 0:
dgts += 1
red //= 10
print(dgts)
But also this this can been seen as a typographical question anyway…
n = abs(int(input()))
print(len(str(n)))