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

Was it helpful?

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!

OTHER TIPS

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,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top