need help in problem in python

list_1 = [1,2,3,4,5,…,50]

i.e. index zero should be 1, index one should be 2, index two should be 3 and so on.

Given an input let’s say a, you have to print the number of elements of list_1 which are divisible by a, excluding the element which is equal to a.

Input:
Number a

Output:
In a single line, the number of elements (i.e. the count and not the elements) which are divisible by a

my code in python is :-

list_1 = [i for i in range(1, 51)]
a=int(input())
list_1.remove(a)
count=0
for i in list_1:
if(i%a==0):
count=count+1
print(count)

but output come is this way when input is 12 and output is :-
1
2
3

but i need output 3 only so how to do this please help

you currently have

list_1 = [i for i in range(1, 51)]
a=int(input())
list_1.remove(a)
count=0
for i in list_1:
    if(i%a==0):
      count=count+1
      print(count)

(rescued from the browser Inspect option and turned into code with the 101/010 button), and by your description you want:

list_1 = [i for i in range(1, 51)]
a=int(input())
list_1.remove(a)
count=0
for i in list_1:
    if(i%a==0):
      count=count+1
print(count)

… that is, complete the loop before reporting.

The main thing to learn here is that you are in charge of the indenting. The editor may suggest an indent if you want to stay in the current control structure, but you don’t want to add statements to the if or indeed the for actions. Bring the indent back as shown and the print is then a post-for-loop action. In Python, indents are part of the language.


Having said that - you don’t really need the loop or the reference array; you know (don’t you?) in your example case that there are 4 multiples of 12 up to 50 without counting through all the non-multiples, and you can just subtract off one to discard the instance of 12 itself. There are various ways to do this in Python which I’ll leave you to discover.

Python works on indentation which is not clear in your code by your question but what I can guess that is you may have put print(count) statement in if(i%a==0) condition.