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

Getting "unknown variable x" or "ValueError" when putting a variable with the "type" of "int" in a method's parameter i.e.".remove()"

Why is the name(variable) being considered "unknown variable ‘x’ " by the interpreter and not be taken as the "value" it has.`

list_of_names = [1, 1, 1, 1, 1, 2, 2, 2, 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]
name = 0
for name in range(100):
    counter = list_of_names.count(name)
    while counter > 1:
            list_of_names.remove(name)

print(list_of_names)

`
Output shown:

Traceback (most recent call last):
  File "x", line 6, in <module>
    list_of_names.remove(name)
ValueError: list.remove(x): x not in list

Process finished with exit code 1

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

>Solution :

while counter > 1:
        list_of_names.remove(name)

That loop will (try) to run forever, because you’re not changing counter inside the loop.

If it started at, say, 5, then it will be 5 forever because nothing in the loop changes it. Eventually you will remove the last occurrence of name from the list, and then the next loop iteration will crash.

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