+ 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 Antworten
+ 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))]