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