[Tutor] Python problem

Roel Schroeven roel at roelschroeven.net
Sat Feb 27 11:19:11 EST 2021


Alan Gauld via Tutor schreef op 27/02/2021 om 12:33:
> On 27/02/2021 11:19, Alan Gauld via Tutor wrote:
>> On 27/02/2021 07:23, Vakhtang Kvaratskhelia wrote:
>>
>>> What i mean is that if i write the code (for simplicity i have
>>> precalculated variables mentioned in the previous emails and narrowed the
>>> code down to the following ) :
>>
>> I'm confused. I'd expect both versions to give an infinite loop since
>> you never change portion_saved and it is equal to zero. So the while
>> loop will always evaluate to the same result.
> 
> OOPS! I just spotted that you do in fact modify the portion_saved.
> Ignore that.
> 
> But i'm still confused about how the commas are working. If someone with
> a comma sensitive locale could explain I'd be interested.

The meaning of Python source code (or any programming language, I would 
hope) does not change depending on locale. My locale uses comma as 
decimal point; to demonstrate that Python is smart enough to ignore that 
and use the point as decimal point:

 >>> 3.14 + 2.7
5.84

Consequence is that Vakthangs code doesn't work, in exactly the way you 
predict:

 >>> 250,000 - portion_saved * 566,774
(250, 0, 774)

 >>> abs(250,000 - portion_saved * 566,774)
Traceback (most recent call last):
   File "<pyshell#2>", line 1, in <module>
     abs(250,000 - portion_saved * 566,774)
TypeError: abs() takes exactly one argument (3 given)

-- 
"Honest criticism is hard to take, particularly from a relative, a
friend, an acquaintance, or a stranger."
         -- Franklin P. Jones

Roel Schroeven



More information about the Tutor mailing list