Mailing List Archive

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
RE: A problem with str VS int. [ In reply to ]
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@python.org> On
Behalf Of Steve GS via Python-list
Sent: Saturday, December 9, 2023 9:42 PM
To: python-list@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

--
https://mail.python.org/mailman/listinfo/python-list
Re: A problem with str VS int. [ In reply to ]
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:
# .....

--
https://mail.python.org/mailman/listinfo/python-list
Re: A problem with str VS int. [ In reply to ]
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/mailman/listinfo/python-list
RE: A problem with str VS int. [ In reply to ]
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

--
https://mail.python.org/mailman/listinfo/python-list
Re: A problem with str VS int. [ In reply to ]
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

--
https://mail.python.org/mailman/listinfo/python-list
Re: A problem with str VS int. [ In reply to ]
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

--
https://mail.python.org/mailman/listinfo/python-list
RE: A problem with str VS int. [ In reply to ]
Roel,

I sent a similar reply in private to someone who may not be listening as their mind is made up. This points to a serious problem with people not testing hypotheses adequately.

Perhaps for homework, we can assign a request for a Python program that creates a random sample of quite a few digit strings of lengths from say 1 to 5 and compares then to each other as strings and then as integer representations and prints out whether the two methods match or not. Perhaps that might get them to discard the hypothesis and be a bity more open.

I still have had to deal with people who want to know why "two" is more than "three" and truly do not understand that just because a human sees "two" as a number, that does not mean anything about another human in whose language it may be "zwei" let alone a computer program not expecting character strings to mean anything unless programmed to examine them a certain way. And often, the same people cannot sole a simple puzzle like "SEND" + "MORE" == "MONEY"

-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On Behalf Of Roel Schroeven via Python-list
Sent: Tuesday, December 12, 2023 3:58 AM
To: python-list@python.org
Subject: Re: A problem with str VS int.

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

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

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