+ 1
Write a program that takes an arbitrary positive integer as input and calculates and prints the number of digits 0 to 9 in it.
can you help me with this?
2 Answers
+ 5
To count the digits of a number several approaches can be done.
âȘïžusing a for loop:
lst = list(map(int, input().split() or '14760645345'))
if all(str(dig).isdigit() for dig in lst): # check if input are all digits
for dig in sorted(set(lst)):
print(f'digit {dig} - {lst.count(dig)}')
âȘïžusing a list comprehension:
# or with a list comprehension:
lst = list(map(int, input().split() or '14760645345'))
[print(f'digit {dig} - {lst.count(dig)}') for dig in sorted(set(lst))]