Answer the question
In order to leave comments, you need to log in
Explain the error in a simple task [Python]?
Hello! Can you please tell me why this code for this task does not work correctly?
N numbers are given: first the number N is entered, then exactly N integers are entered. Count the number of zeros among the entered numbers and output this number. You need to count the number of numbers that are zero, not the number of digits.
print('Введите количество чисел: ')
N = int(input())
for i in range(N):
print('Введите число: ')
n = int(input())
for i in range(n):
kol=0
if i==0:
kol+=1
print(kol)
Answer the question
In order to leave comments, you need to log in
What for
for i in range(n):
kol=0
if i==0:
kol+=1
print(kol)
print('Введите количество чисел: ')
N = int(input())
kol=0
for i in range(N):
print('Введите число: ')
n = int(input())
if n==0:
kol+=1
print(kol)
Well, the main problem is that not then you add 1 to the counter, in fact, this has already been said.
If you want to be shorter and simpler (so as not to think when to do +1), then you can do this:
n = int(input('Введите количество чисел: '))
numbers = [int(input('Введите число: ')) for i in range(n)]
print(numbers.count(0))
Solutions to the problem have already been given. But here's another tip, try to use generators in python and methods of built-in types, it's very convenient, everything has already been done for you, you just need to choose the right tools.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question