如果0是第一个数字,代码不计算0。例如,如果输入是012,则返回0而不是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}")
1条答案
按热度按时间yx2lnoni1#
您正在将int转换为string,然后再转换回int。这将导致0被删除,因为012实际上只是12。
尝试