G+_Tadeusz Cantwell Posted May 8, 2014 Share Posted May 8, 2014 Trying to write code that tests if 1-10 has been entered. After some searching found this works if 0 <= letter_replace <= 10: After begining with Java. Mind blown! Not sure how to read precedence/starting point in that code. Link to comment Share on other sites More sharing options...
G+_Darryl Medley Posted May 9, 2014 Share Posted May 9, 2014 Actually should be "0 < letter_replace" instead of "0 <= letter_replace" because "<=" evaluates to True if letter_replace is 0 and your range is 1-10. Link to comment Share on other sites More sharing options...
G+_egbie anderson Posted May 9, 2014 Share Posted May 9, 2014 The code above is incomplete as there are no post conditions to determine what should occur if the value is True or False But the code itself is known as an inline if-statement and considered pythonic for short if statements. The code is fairly easy to read . It states that if the variable letter_replace is greater or equal to zero but less or equal to ten. But here the thing not only is the code incomplete it would test for numbers between 0 and 10 instead of 1 and 10. This is because of the "0 <=" statement. Which means greater or equal to 0. Since you mentioned in your above post that you are only interested in testing numbers between 1 and 10. The code above would produce the wrong results. I have rewritten the code and placed it inside a function called test. I have renamed the variable letter_replace to num to better represent the problem at hand. I have not included any error checking or as they say defensive programming. I leave that up to you. def test(num): '''num(int) -> return(bool) Takes a number and test if the number is between 1 and 10. If True a bool value of True is returned and False otherwise. >>> test(1) True >>> test(10) True >>> test(0) False >>> test(-1) False >>> test(11) False ''' return(True if 0 < num <= 10 else False) The function returns True for all numbers between 1 and 10 but returns False if the criteria are not met Not this should only be used for short boolean clauses. Any thing longer then one line should not be used as it becomes clustered and hard to read. Link to comment Share on other sites More sharing options...
G+_L I Posted May 9, 2014 Share Posted May 9, 2014 egbie anderson a bit of overkill, no? Link to comment Share on other sites More sharing options...
G+_L I Posted May 9, 2014 Share Posted May 9, 2014 I'm surprised that Python allows something like (a < b < c )evaluation but doesn't allow a++ :) Link to comment Share on other sites More sharing options...
G+_Tadeusz Cantwell Posted May 9, 2014 Author Share Posted May 9, 2014 That's a good point about just having the > and not the =. In previous code I had the test in a function and then called the function in a while loop so it only returned true if the condition was met. I presume defensive programming and sanitizing code are the same thing. Link to comment Share on other sites More sharing options...
G+_egbie anderson Posted May 9, 2014 Share Posted May 9, 2014 Tadeusz Cantwell You are right defensive programming and santizing code are the same thing. Defensive programming is just a widely use term in the world of programming. There both based on the assumption that the user of the program might not enter the right data. And also that a library call or a particular function might not work. And in the event that this happens steps are taken to ensure that an error does not occur or even worse a security vunerability. a.ka. heartbleed. Link to comment Share on other sites More sharing options...
G+_Lee Crocker Posted May 9, 2014 Share Posted May 9, 2014 Yeah, the bound-check comparison idiom is a Python thing. I don't use it, just as I never use ++, because they confuse people reading the code, which is not worth saving a tiny bit of typing. Just because a language has a feature, that doesn't mean it's good to use. Different programmers will have different opinions just as different writers do about words. Also "defensive programming" has a pejorative meaning different from merely sanitizing your inputs--it can also refer to the practice of writing your programs to continue running in the face of bad inputs rather than failing, which is a bad thing. Link to comment Share on other sites More sharing options...
G+_Darryl Medley Posted May 9, 2014 Share Posted May 9, 2014 We seem to be talking about 2 different types of expressions. Both are mentioned in the doc: https://docs.python.org/2/reference/expressions.html Section 5.9 shows the form Tadeusz mentioned (x < y <= z) and Section 5.11 describes Conditional Expressions, aka "Inline If," aka "Immediate If" (as us old dBase coders call it) that egbie describes. I didn't know Python had either one of these forms of expressions so this discussion has been great. Link to comment Share on other sites More sharing options...
G+_Carlos Urrutia Posted May 11, 2014 Share Posted May 11, 2014 Lee Crocker I disagree with the ++ operator being confusing. If a simple ++ makes the code confusing then the code is already messy and needs to be cleaned up. Link to comment Share on other sites More sharing options...
G+_Lee Crocker Posted May 11, 2014 Share Posted May 11, 2014 The trouble with ++ is that people use it in non-simple ways. Link to comment Share on other sites More sharing options...
Recommended Posts