You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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.
So, the feedback for choice B may need a little tweak.
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.
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.
The text was updated successfully, but these errors were encountered: