When you have an error, always see the error message that appears on the terminal. If you don’t find out what’s going on there, when asking a question, include the error message along with your code.
In that case, you would have seen that the message is like
Valueerror: list.remove(x): x not in list
i.e.: your program stopped with an error because the element you tried to remove from the list using remove
was not on the list.
The simplest way to resolve the error (although your program might have other improvements) is to test for the presence of the element before trying to remove it:
oldpos = [int(oldPosX), int(oldPosY)]
if oldpos in obstacles:
obstacles.remove([int(oldPosX), int(oldPosY)])
That will eliminate this mistake. As to why he is not finding the set of coordinates, there may be several: but I suggest starting to see in your code if you rounded them up for integers when placing them on the list.
Another hint about code: removing list elements with "remove" can be inefficient - why Python has to go through the entire list before removing the element. If your "obstacles" don’t need to know about the order of the coordinates inside, use a set
(set) instead of a list - (and use tuple
and not list
for the coordinate pairs inside): the time to remove an element from a set
is constant.
Tip to post questions: always put the entire class and function statement with the necessary context so we can answer a question - then:
obstcales = []
class Nome(object):
...
def minhafunc(self, arg1, arg2):
...
if event == 'position':
...
It is very difficult to answer about "position" if we do not know how it was declared, and what is inside it. If the code is small, try to put all the code. Unlike other languages where you need a lot of code, always the same, to get to the "core" where something happens, in Python the "Boiler Plate" is minimal: it helps a lot to see the whole context.
And finally, it does not cost to reperte: always, always see the error message that Python plays in the terminal when a program gives error. It never simply "to the server". (Except in rare cases where a segmentation failure occurs in a third-party module)
You don’t need to - and shouldn’t - do things like
self.__getattribute__('positionX')
- onlyself.positionX
has the same effect. If by chance, you have the name"positionX"
in a variable and you want to use the variable, usegetattr(self, <variável>)
)– jsbueno