Issue
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}")
Solution
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
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.