Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Read a positive integer (N) from user input. Write a for loop to count and print the number of 0's in N.
 
Note: must use the for loop.
 
Hint:
in; if ...;

 
Example 1:
Input:

20020200
Output:

5

What I have tried:

<pre># read an integer

n = int(input())

# initialize count

count = 0

# write a for loop with proper iterable
for i in str(n):
    if i == 0:
        count = count + 1
    else:
        continue

print(count)
Posted
Updated 22-Oct-20 6:06am

1 solution

You read a string and convert it to an integer value. Why do you then convert it back to a string? And in your loop you test if the character is equal to the integer value 0, which will never be true. All you need is:
Python
# initialize count
count = 0
# write a for loop with proper iterable
number = input()
for i in number:
    if i == '0':
        count = count + 1

print(count)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900