0

Count the number of zero's

The program should output how many zeros are there! here is my code! what is wrong with it?? n = int(input()) sum = 0 for i in range(n): if n == 0: sum = sum+1 print(sum) else: print(0) You can check my code in this link: https://snakify.org/en/lessons/for_loop_range/problems/how_many_zeroes/ WARNING: just tell me why my code doesn't output the right thing pleaseee

12th Feb 2021, 7:15 PM
Ailana
Ailana - avatar
7 Antworten
+ 1
Can you add what is your expected output for a sample output? Your this code prints n number of 0s only..(by else part) edit: Ailana from the link, it is expected to read n values and check how many of them are 0s. but you are reading only 1st input.
12th Feb 2021, 7:34 PM
Jayakrishna 🇮🇳
+ 1
You are just taking the first input which is N. There are no other inputs. N = int(input()) zeroCount = 0 for n in range(N): num = int(input()) if(num == 0): zeroCount = zeroCount+1 print(zeroCount)
12th Feb 2021, 7:57 PM
Avinesh
Avinesh - avatar
+ 1
For sample input : 3 4 0 1 Your code : n = int(input()) #this reads 3 sum = 0 for i in range(n): #this repeated i =0 to 2 if n == 0: #here 3==0 never true and executes else part. sum = sum+1 print(sum) else: print(0) What you need is : in loop read remaining inputs and check if it is 0 or not. After out of loop print count value. Ailana if you want ,To understand how loop works ,run this code and see output. n = 10 for i in range(n): print(i) #outputs 0 to 9
12th Feb 2021, 8:23 PM
Jayakrishna 🇮🇳
0
range(n) generates a sequence 0, 1, 2, ..., n That's not what you need Treat the input as string, so no int() around it. Then loop through the characters: for char in n:
12th Feb 2021, 7:32 PM
Benjamin Jürgens
Benjamin Jürgens - avatar
0
Can u give me an example Jayakrishna? Im sorry if i dont understand some things you tell me: :<
12th Feb 2021, 7:40 PM
Ailana
Ailana - avatar
0
N -> this will tell you the number of input values that are going to follow. N=5 means there is going to be 5 numbers. 5 18 0 67 0 48 This means there are 5 numbers. 18, 0, 67, 0, 48 and you must check that how many of them are 0's.
12th Feb 2021, 7:54 PM
Avinesh
Avinesh - avatar
0
I know but what is wrong with my code up there on my question
12th Feb 2021, 7:55 PM
Ailana
Ailana - avatar