Write a program to accept a number from the user and count the zeros, odd digits and non-zero even digits from the entered number

Question:

The code doesn’t count the 0 if 0 is the first digit. For example, if the input is 012 it returns the number of zeros as 0 instead of 1.

n=int(input())
str_number = str(n)
zero = 0
odd = 0
even = 0
for i in str_number:
    if int(i) == 0:
        zero += 1
    elif (int(i)%2) == 0:
        even += 1
    else:
        odd += 1
print(f"Number of odd digits:{odd}")
print(f"Number of non-zero even digits:{even}")
print(f"Number of zeros:{zero}")
Asked By: Joe

||

Answers:

You are converting int, to string, back to int.
This causes the 0 to be removed because 012 is actually just 12.

try

n=str(input())

#do stuff
for i in n:
    #do more stuff
Answered By: Shaun Graham
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.