0

我有以下代码:

while current is not problem.getStartState():

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

现在由于某种原因,比较效果不佳,您可以在以下输出中看到:

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)

您可以看到,即使 current 与 getStartState() 相同,它也会进入 while。此外 - 当它曾经是一个 2 字段元组 (x,y) 时,它工作得很好。

我究竟做错了什么 ?谢谢

4

3 回答 3

5

is测试身份,而不是平等。你要current != problem.getStartState()

有一个成语is (not) None有效,因为None保证是单例。除非您是认真的,否则不要将其用于其他类型!

于 2011-12-20T15:25:52.620 回答
2
while current != problem.getStartState():

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

is是身份(相同对象)比较器。在您的情况下,您需要一个相等(或不等式)(具有相同值的对象)运算符。

于 2011-12-20T15:25:11.417 回答
-1

is不是在这种情况下使用的正确检查。要比较 2 个元组,只需使用 != 或 ==

for instance the problem can be solved as follows:

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

cheers,

于 2011-12-20T15:32:40.313 回答