• A problem with str VS int.

    From Steve GS@21:1/5 to All on Sat Dec 9 21:42:06 2023
    If I enter a one-digit input or a three-digit number, the code works but if I enter a two digit number, the if statement fails and the else condition prevails.

    tsReading = input(" Enter the " + Brand + " test strip reading: ")
    if tsReading == "": tsReading = "0"
    print(tsReading)
    if ((tsReading < "400") and (tsReading >= "0")):
    tsDose = GetDose(sReading)
    print(tsReading + "-" + tsDose)
    ValueFailed = False
    else:
    print("Enter valid sensor test strip Reading.")

    I converted the variable to int along with the if statement comparison and it works as expected.
    See if it fails for you...

    Steve

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dn@21:1/5 to Steve GS via Python-list on Sun Dec 10 18:53:11 2023
    On 10/12/23 15:42, Steve GS via Python-list wrote:
    If I enter a one-digit input or a three-digit number, the code works but if I enter a two digit number, the if statement fails and the else condition prevails.

    tsReading = input(" Enter the " + Brand + " test strip reading: ")
    if tsReading == "": tsReading = "0"
    print(tsReading)
    if ((tsReading < "400") and (tsReading >= "0")):
    tsDose = GetDose(sReading)
    print(tsReading + "-" + tsDose)
    ValueFailed = False
    else:
    print("Enter valid sensor test strip Reading.")

    I converted the variable to int along with the if statement comparison and it works as expected.
    See if it fails for you...

    It works as expected (by Python)! This is how strings are compared -
    which is not the same as the apparently-equivalent numeric comparison.

    Think about what you expect from the code below, and then take it for a
    spin (of mental agility):

    values = [ 333, 33, 3, 222, 22, 2, 111, 11, 1, ]
    print( sorted( values ) )
    strings = [ "333", "33", "3", "222", "22", "2", "111", "11", "1", ]
    print( sorted( strings ) )


    The application's data appears numeric (GetDose() decides!).
    Accordingly, treat it so by wrapping int() or float() within a
    try-except (and adjusting thereafter...).


    "But wait, there's more!"
    (assuming implement as-above):

    if 0 <= ts_reading < 400:

    1 consistent 'direction' of the comparisons = readability
    2 able to "chain" the comparisons = convenience
    3 identifier is PEP-008-compliant = quality and style

    --
    Regards,
    =dn

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thomas Passin@21:1/5 to Steve GS via Python-list on Sun Dec 10 00:23:33 2023
    On 12/9/2023 9:42 PM, Steve GS via Python-list wrote:
    If I enter a one-digit input or a three-digit number, the code works but if I enter a two digit number, the if statement fails and the else condition prevails.

    tsReading = input(" Enter the " + Brand + " test strip reading: ")
    if tsReading == "": tsReading = "0"
    print(tsReading)
    if ((tsReading < "400") and (tsReading >= "0")):
    tsDose = GetDose(sReading)
    print(tsReading + "-" + tsDose)
    ValueFailed = False
    else:
    print("Enter valid sensor test strip Reading.")

    I converted the variable to int along with the if statement comparison and it works as expected.
    See if it fails for you...

    I don't have to try it to know it will fail. You think you are
    comparing numbers but you are comparing strings instead, which won't
    work as you expect.

    You would do better to convert the inputs and limits to numbers, as well
    as the GetDose() function. In a more realistic version, you would also
    have to make sure the user input is legal, either an int or a float,
    whichever you want to work with.

    And along those lines (a more realistic version), it would be preferable
    to change the limits to be named constants, which will make the code
    easier to understand and change when it's revisited later. Something
    like this:

    UPPER = 400
    LOWER = 0
    # ...
    if LOWER < value < UPPER:
    # .....

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Steve GS on Sun Dec 10 11:09:33 2023
    "Steve GS" <Gronicus@SGA.Ninja> writes:
    I converted the variable to int along with the if statement
    comparison and it works as expected.

    "2">"10"
    |True

    because, in a dictionary, the word "2" would be inserted after "10".

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Steve GS@21:1/5 to via Python-list on Tue Dec 12 03:22:22 2023
    With all these suggestions on
    how to fix it, no one seems to
    answer why it fails only when
    entering a two-digit number.
    One and three work fine when
    comparing with str values. It
    is interesting that the
    leading 0 on a two digit
    worked. Still, one digit and
    three digit work but not two.

    This is now more of a
    curiosity as I did use the
    integer comparisons.

    SGA

    -----Original Message-----
    From: Python-list
    <python-list-bounces+gronicus=
    sga.ninja@python.org> On
    Behalf Of dn via Python-list
    Sent: Sunday, December 10,
    2023 12:53 AM
    To: python-list@python.org
    Subject: Re: A problem with
    str VS int.

    On 10/12/23 15:42, Steve GS
    via Python-list wrote:
    If I enter a one-digit
    input or a three-digit number,
    the code works but if I enter
    a two digit number, the if
    statement fails and the else
    condition prevails.

    tsReading = input("
    Enter the " + Brand + " test
    strip reading: ")
    if tsReading == "":
    tsReading = "0"
    print(tsReading)
    if ((tsReading <
    "400") and (tsReading >=
    "0")):
    tsDose =
    GetDose(sReading)
    print(tsReading
    + "-" + tsDose)
    ValueFailed =
    False
    else:
    print("Enter
    valid sensor test strip
    Reading.")

    I converted the variable to
    int along with the if
    statement comparison and it
    works as expected.
    See if it fails for you...

    It works as expected (by
    Python)! This is how strings
    are compared - which is not
    the same as the
    apparently-equivalent numeric
    comparison.

    Think about what you expect
    from the code below, and then
    take it for a spin (of mental
    agility):

    values = [ 333, 33, 3, 222,
    22, 2, 111, 11, 1, ] print(
    sorted( values ) ) strings = [
    "333", "33", "3", "222", "22",
    "2", "111", "11", "1", ]
    print( sorted( strings ) )


    The application's data appears
    numeric (GetDose() decides!).
    Accordingly, treat it so by
    wrapping int() or float()
    within a try-except (and
    adjusting thereafter...).


    "But wait, there's more!"
    (assuming implement as-above):

    if 0 <= ts_reading < 400:

    1 consistent 'direction' of
    the comparisons = readability
    2 able to "chain" the
    comparisons = convenience
    3 identifier is
    PEP-008-compliant = quality
    and style

    --
    Regards,
    =dn
    --
    https://mail.python.org/mailma
    n/listinfo/python-list

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Roel Schroeven@21:1/5 to All on Tue Dec 12 09:57:31 2023
    Op 12/12/2023 om 9:22 schreef Steve GS via Python-list:
    With all these suggestions on
    how to fix it, no one seems to
    answer why it fails only when
    entering a two-digit number.
    One and three work fine when
    comparing with str values. It
    is interesting that the
    leading 0 on a two digit
    worked. Still, one digit and
    three digit work but not two.

    Three-digit numbers work because you're comparing to another three-digit numbers. When two integer numbers have the same number of digits, their lexicographical ordering matches their numeric ordering.

    One-digit numbers don't work fine:

    "5" < "400"
    False

    even though we can construct cases where it seems as if they do:

    "1" < "400"
    True

    Two-digit numbers sometimes seem to work:

    "30" < "400"
    True

    But other times clearly don't work:

    "50" < "400"
    False

    String comparison first looks at the first characters of both operands.
    If they are different (as in the examples above), their ordering is used regardless of all the other characters that come after, and regardless
    of the length of the string. Try working through some examples (make
    sure to pick examples with a wide variety of first digits) and you'll
    see why it sometimes seems to work, but very unreliably.

    --
    "In the old days, writers used to sit in front of a typewriter and stare out of the window. Nowadays, because of the marvels of convergent technology, the thing
    you type on and the window you stare out of are now the same thing.”
    -- Douglas Adams

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dn@21:1/5 to Steve GS on Tue Dec 12 22:08:58 2023
    On 12/12/23 21:22, Steve GS wrote:
    With all these suggestions on
    how to fix it, no one seems to
    answer why it fails only when
    entering a two-digit number.
    One and three work fine when
    comparing with str values. It
    is interesting that the
    leading 0 on a two digit
    worked. Still, one digit and
    three digit work but not two.

    This is now more of a
    curiosity as I did use the
    integer comparisons.


    Emphasis on the word "seems"!

    Did you try running the code provided earlier? Did you notice that such illustrated what happens when using strings which appear as numbers, and
    showed how a three digit number (expressed as a string) may well precede
    a two- (or even a one-) digit number in the same form - and that the
    end-result of the sequence of integers is quite-different to the
    sequence of integer-values expressed as strings!

    Why does this happen? Because of the way data is encoded. It is language independent. The definition of order or sequence is called "collation".

    "Comparisons" establish relative-sequence. You will find some handy explanations of how comparisons work (eg equals or less-than) in the
    Python manual.

    After working through those three steps, if there's something that's
    still mystifying, please refine the question...


    Web.Refs:
    https://en.wikipedia.org/wiki/Collation https://docs.python.org/3/reference/expressions.html?highlight=comparison#value-comparisons
    https://docs.python.org/3/howto/sorting.html?highlight=sort


    --
    Regards,
    =dn

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)