A problem with str VS int.

avi.e.gross at gmail.com avi.e.gross at gmail.com
Tue Dec 12 10:35:55 EST 2023


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 at python.org> On Behalf Of Roel Schroeven via Python-list
Sent: Tuesday, December 12, 2023 3:58 AM
To: python-list at 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



More information about the Python-list mailing list