T
T
Timebird2016-02-05 11:35:22
Python
Timebird, 2016-02-05 11:35:22

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

4 answer(s)
O
Oscar Django, 2016-02-05
@Timebird

What for

for i in range(n):
    kol=0 
    if i==0: 
        kol+=1 
print(kol)

Just
print('Введите количество чисел: ')
N = int(input())
kol=0
for i in range(N):
  print('Введите число: ')
  n = int(input())
  if n==0: 
    kol+=1 
print(kol)

I
Ilya, 2016-02-05
@766dt

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))

T
trnc, 2016-02-05
@trnc

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.

A
abcd0x00, 2016-02-05
@abcd0x00

>>> def f():
...     return sum(int(input()) == 0 for _ in range(int(input())))
... 
>>> print(f())
5
0
2
0
2
0
3
>>>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question