Question

I have the following piece of code:

while current is not problem.getStartState():

        print "Current: ", current, "Start: ", problem.getStartState()

now for some reason the comparison is not working well, you can see in the following output:

Current:  (3, 5, 0, 0, 0, 0) Start:  (4, 5, 0, 0, 0, 0)
Current:  (4, 5, 0, 0, 0, 0) Start:  (4, 5, 0, 0, 0, 0)

you can see that even though current is the same as getStartState() it enters the while. furthermore - when it used to be a 2 fields tuple (x,y) it worked fine.

What am I doing wrong ? Thanks

Était-ce utile?

La solution

is tests for identity, not equality. You want current != problem.getStartState()

There is an idiom is (not) None which works because None is guaranteed to be a singleton. Don't use it for other types unless you really mean it!

Autres conseils

while current != problem.getStartState():

    print "Current: ", current, "Start: ", problem.getStartState()

is is the identity (same objects) comparator. In your case, you need an equality (or inequality) (objects with same values) operator.

is is not the correct check to be used in this case. To compare 2 tuples just use != or ==

for instance the problem can be solved as follows:

while current != problem.getStartState():   
        print "Current: ", current, "Start: ", problem.getStartState()

cheers,

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top