A problem with str VS int.

avi.e.gross at gmail.com avi.e.gross at gmail.com
Sat Dec 9 23:23:10 EST 2023


Steve,

I would say converting to a number, as you eventually did, is the way to go.

When you compare character strings, it will not be in numeric order. Compare
"80" with "400" and since 8 is greater than 4, the comparison is over and
"80" is greater then "40" even though 80 is way less than 400.

The only way to get the kind of comparison you want would be to pad with
zeroes so all your "numbers" are the same length. In that case, "080" would
indeed test as less than "400" but rarely does that seem a good idea. If the
user can enter any text, they might enter ".01" or "hello" or al kinds of
nonsense.

If you converted to numbers and tested whether it failed, ...

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com at python.org> On
Behalf Of Steve GS via Python-list
Sent: Saturday, December 9, 2023 9:42 PM
To: python-list at python.org
Subject: A problem with str VS int.

 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

-- 
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list