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

Reply via email to