Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I have troubles with counting the list elements equal to a given value K

I used the command list.count, but it just doesn’t work.
Here’s the code:

import random

a = [random.randint (1, 10) for c in range (20) ]
print (a)

d = int (input ('Input a number') )
a.count(d)

while d in a:
    list.remove (d)
print(a)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

you have to remove d from a so you would do a.remove(d) and not list.remove(d)

list is an object of a class and not an instance of it, the list you are searching for is not list, but a:

import random
a = [random.randint (1, 10) for c in range (20) ]
print (a)
d = int (input ('Input a number') )
# a.count(d) #doesn't do anything
while d in a:
    a.remove(d) #remove d from a
print(a)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading