Posted By

a7xrturo on 11/23/13


Tagged

counter digit


Versions (?)

Digit counter


 / Published in: Python
 

Program that asks for a number and then a digit. Then it shows how many times the digit is in the given number.

  1. print('-- Digit counter --')
  2.  
  3. # get inputs
  4.  
  5. num = input('Enter a number > ')
  6.  
  7. while not num.isdigit():
  8. print('Number must be a positive integer! Try again.')
  9. num = input('Enter a number > ')
  10.  
  11. print('Number entered is {}'. format(num))
  12.  
  13. dig = input('Enter a digit > ')
  14.  
  15. while True:
  16. if dig.isdigit():
  17. if int(dig) in range(0, 10):
  18. break
  19. else:
  20. print('Digit must be in the range 0...9 Try again.')
  21. dig = input('Enter a digit > ')
  22. else:
  23. print('Digit must be in the range 0...9 Try again.')
  24. dig = input('Enter a digit > ')
  25.  
  26. print('The digit entered is {}'.format(dig))
  27.  
  28. # increments counter every time digit is found in number
  29. counter = 0
  30.  
  31. for i in num:
  32. if dig == i:
  33. counter += 1
  34.  
  35. print('The number of {0} \'s in {1} is {2}'.format(dig, num, counter))

Report this snippet  

You need to login to post a comment.