简体   繁体   English

评估Python中比较运算符的行为

[英]Evaluation of comparison operators in Python behaving unexpectedly

I'm TAing an introductory course on Python this semester (using 3.4) and recently came across an exercise about operator precedence and using parentheses to make a statement evaluate to true. 我本学期正在使用Python入门课程(使用3.4),并且最近遇到了关于运算符优先级和使用括号使语句评估为true的练习。

The exact question is: 确切的问题是:

Add a single pair of parentheses to the expression so that it evaluates to true.
1 < -1 == 3 > 4

I assumed that the correct answer would be: 我认为正确的答案是:

1 < -1 == (3 > 4)

Given that the comparison operators are all on the same level of precedence, they should evaluate from left to right, so it should evaluate as such: 假定比较运算符都处于相同的优先级,则它们应该从左到右求值,因此它应该这样求值:

1 < -1 == (3 > 4) 
1 < -1 == False
False == False
True

But when I run the code it still returns false. 但是,当我运行代码时,它仍然返回false。 I saw this question comparison operators' priority in Python vs C/C++ and the result of that expression makes sense to me; 在Python和C / C ++中看到了这个问题比较运算符的优先级 ,该表达式的结果对我来说很有意义。 but in this case I've forced the evaluation of the latter statement before evaluating the rest of the expression, so I don't get why I'm still getting the wrong answer. 但是在这种情况下,我不得不在评估表达式的其余部分之前先评估后一个语句,所以我不明白为什么我仍然得到错误的答案。

I've been staring at it for the past hour and I feel like I might be overlooking something obvious; 在过去的一个小时里,我一直在盯着它看,感觉好像我可能忽略了一些明显的事情。 if anyone could provide some insight as to what the correct solution might be it'd be greatly appreciated. 如果有人可以提供关于什么是正确解决方案的见解,将不胜感激。

The task is provably impossible. 任务证明是不可能的。 Consider these three cases: 考虑以下三种情况:

  1. There is an open paren immediately before -1 . -1之前有一个开放的paren。

  2. There is an open paren between - and 1 . -1之间有一个开放的括号。

  3. There is an open paren anywhere else. 在其他任何地方都有开放式的父母。

These three cases represent every possible location for the parentheses. 这三种情况代表括号的所有可能位置。

In the first case, we have 1 < ( ... ) , where the ellipsis is a boolean expression. 在第一种情况下,我们有1 < ( ... ) ,其中省略号是布尔表达式。 Since 1 is not less than either True or False , the entire expression is False . 由于1不小于TrueFalse ,因此整个表达式为False

In the second case, we have 1 < -( ...) , where the ellipsis is a boolean expression. 在第二种情况下,我们有1 < -( ...) ,其中省略号是布尔表达式。 Since 1 is not less than either -True nor -False , the entire expression is False . 由于1不小于-True-False ,因此整个表达式为False

In the third case, we have 1 < -1 == ... . 在第三种情况下,我们有1 < -1 == ... Since all legs of a expression of chained operators must be True, and and since 1 < -1 is False, the entire expression is False . 由于链接运算符的表达式的所有分支都必须为True,并且由于1 < -1为False,因此整个表达式为False

So, in every possible case, the result is False. 因此,在每种可能的情况下,结果都是False。

You are on the right track, but due to comparison chaining you will have: 您的方向正确,但是由于比较链的原因,您将拥有:

1 < -1 == (3 > 4) 
1 < -1 == False
1 < -1 and -1 == False
False and False
False

Notice that we don't evaluate the second line left to right, rather we chain the two comparisons together with an and. 注意,我们不评估从左到右的第二行,而是将两个比较与and链接在一起。

There are only a few valid ways to add one pair of parentheses to this, so we can check all the others easily: 只有几种有效的方法为此添加一对括号,因此我们可以轻松地检查所有其他括号:

(1 < -1 == 3 > 4)
False #Trivially

(1 < -1) == 3 > 4
False == 3 > 4
False == 3 and 3 > 4
False and False
False

1 < (-1 == 3) > 4
1 < False > 4
1 < False and False > 4
False and False
False

It looks like there is no answer to this problem! 看来这个问题没有答案!

Edit: 编辑:

Whoops! 哎呀! Rob points out we forgot: Rob指出我们忘记了:

1 < -(1 == 3) > 4
1 < -False > 4
1 < 0 > 4
1 < 0 and 0 > 4
False and False
False

3>4 gives False . 3>4给出False -1 == False gives False . -1 == False给出False 1 < False gives False . 1 < False给出False Hence, 1 < -1 == 3 > 4 becomes False . 因此, 1 < -1 == 3 > 4变为False

Give proper brackets to make the statement semantically correct. 给出适当的括号以使该语句在语义上正确。

(1 < -1) == (3 > 4)

(1 < -1) == (3 > 4) (1 <-1)==(3> 4)

False == False 假==假

True 真正

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM