loop help
Steven D'Aprano
steve at REMOVETHIScyber.com.au
Thu Oct 27 08:30:31 EDT 2005
On Thu, 27 Oct 2005 07:00:34 +0000, Gorlon the Impossible wrote:
> Hello. I am using Python 2.3.5 with IDLE 1.0.5 on a Windows98 PC.
> I have some programming experience but mostly I am still learning.
> I am having some trouble understanding the behaviour of a piece of
> code I have written. It plots points using PIL.
> Here is my loop:
>
> triangle = [(320,27),(172,323),(468,323)]
> currentpoint = (randint(0,640),randint(0,350))
> t = 0
> while t < 10:
Let me guess... you're a C programmer, right?
This is bad practice. The preferred idiom is to do something like
this:
for t in range(10):
# loop
Why manage your own loop variable, inefficiently duplicating code that
already exists in Python?
> ct = choice(triangle)
> mp = (currentpoint[0] + ct[0])/2, (currentpoint[1] + ct[1])/2
> draw.point(mp,(0,0,0))
> currentpoint = mp
> t = t + 1
>
> This works fine. But, if I try to divide by a floating point number in
> the calculation of mp, instead of mp being overwritten each time thru
> the loop, it starts accumulating, leading to overflow errors for large
> values of t.
> Why is this?
Please explain your problem first. In particular, show us the code that
you use that "it (what?) starts accumulating".
--
Steven.
More information about the Python-list
mailing list