Nested while-s

Tim Peters tim_one at email.msn.com
Fri Jan 14 23:30:43 EST 2000


[Josh Tompkins]
> Is it not possible to nest while's?

Sure!

> Consider this code:
>
> def listOfRadius(radius, xpos, ypos):
> 	list = []
> 	currentX = (xpos - radius)
> 	currentY = (ypos - radius)
> 	while currentX <= (xpos + radius):
> 	    while currentY <= (ypos + radius):
> 		    str1 = `currentX` + " , " + `currentY`
> 		    list.append(str1)
> 		    currentY = currentY + 1
> 	    currentX = currentX + 1
> 	return list

Note that you never reset currentY to ypos-radius, so the inner loop won't
be entered on any but the first iteration of the outer loop.  You probably
want to change

    currentY = (ypos - radius)
    while currentX <= (xpos + radius):

to

    while currentX <= (xpos + radius):
        currentY = (ypos - radius)

(that is, just swap those two lines).

feel-free-to-kick-yourself-now<wink>-ly y'rs  - tim






More information about the Python-list mailing list