Home > other >  Digit count program in python
Digit count program in python

Time:12-11

This is a program which counts the no. of digits a user input in the programm.

n=int(input("Your no. :"))
dcount=0 
while n>0:
     dcount=dcount 1
print(dcount)

I make a while loop , was expecting the result but insted of counting the digits of the user input no. ,the output is showing nothing.

CodePudding user response:

If you want to count amount of digits in presented integer, you should divide n until n > 0, so your condition will work.

n = int(input("Your no. :"))
amount = 0 
while n > 0:
    n //= 10
    amount  = 1
print(amount)

CodePudding user response:

because the code was incomplete. print will only work when loop stop. in your code the loop will never stop, because the n value didn't come to zero. so you add n//= 10 inside loop. it will work. and you tell me what this n//= 10 do.

CodePudding user response:

need to break the cycle somewhere

  • Related