Jump to content

Trying to write code that tests if 1-10 has been entered After some searching found this works


G+_Tadeusz Cantwell
 Share

Recommended Posts

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

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

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

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

 Share

×
×
  • Create New...