fopp icon indicating copy to clipboard operation
fopp copied to clipboard

question7_3_1: 0 < x < 5 -- "you should not use it" -- why?

Open yarikoptic opened this issue 7 years ago • 3 comments

it is about

condition-3-1: What is the correct Python expression for checking to see if a number stored in a variable x is between 0 and 5. A. x > 0 and < 5 B. 0 < x < 5 C. x > 0 or x < 5 D. x > 0 and x < 5

and the answer is D, while B. having following feedback

This is tricky. Although most other programming languages do not allow this syntax, in Python, this syntax is allowed. However, you should not use it. Instead, make multiple comparisons by using and or or.

Since feedback is actually not giving an explanation on "Why" it shouldn't be used, and in my experience I do not frequently use this construct, I do not remember what could possibly discourage it. On the opposite, I found some (eg) somewhat encourage it as a neat Python feature making code closed to regular written math.

yarikoptic avatar Jan 19 '19 02:01 yarikoptic

That's a good point. In my perspective, using logical operator is more of an implementation way of thinking than "dry" solving of Math expression. Moreover, using logical operator in expression is more readable, so more practiced?

Also looked up to see if there are any other articles/style guide that talks about to use Math expression or logical operators in Python expression, couldn't find.

Also the article (in confusing manner?) states that logical operator is used in expression when some variable is complex and rather not repeat.

screen shot 2019-01-29 at 10 37 18 am

So, the feedback for choice B may need a little tweak.

dichharai avatar Jan 29 '19 18:01 dichharai

Made PR #195 for the issue.

dichharai avatar Jan 30 '19 19:01 dichharai

It is usual to use x > 0 and x < 5 for checking if x is between the two numbers. However, 0 < x < 5 is still an equally viable solution to the problem meaning this question could be converted into a multiple answers problem where B and D are both correct answers. I'm currently working on this issue.

RustyDotson avatar Sep 25 '19 18:09 RustyDotson