and always draw the polygon created so far when click goes outside rectstop and return the final polygon the polygon ends up drawn the method draws and undraws rect ''it is useful to start by thinking of the objects neededand give them names polygon is needed call it poly list of vertices is needed call it vertices need to append to this list it must be initialized first the latest mouse click point is needed call it pt certainly the overall process will be repetitiouschoosing point after point still it may not be at all clear how to make an effective python loop in challenging situations like this it is often useful to imagine concrete situation with limited number of stepsso each step can be written in sequence without worrying about loop for instance to get up to triangle ( vertices in our list and fourth mouse click for the sentinel)you might imagine the following sequenceundrawing each old polygon before the next is displayed with the latest mouse click includedrect setoutline('red'rect draw(winvertices list(pt win getmouse(vertices append(ptpoly polygon(verticespoly draw(winwith one point pt win getmouse(poly undraw(missing latest point vertices append(ptpoly polygon(verticespoly draw(winwith two points pt win getmouse(poly undraw(missing latest point vertices append(ptpoly polygon(verticespoly draw(winwith three points pt win getmouse(assume outside the region rect undraw(return poly there is fine point here that missed the first time the vertices of an existing polygon do not get mutated in this system new polygon gets created each time with the new vertex list the old polygon does not go away automaticallyand extraneous lines appear in the picture if the old polygon is not explicitly undrawn each time before new version is redrawn with an extra vertex the last polygon you draw should be visible at the endso in the example above where was assuming the third click was the last for the trianglei did not undraw the polygon the timing for each undraw needs to be after the next mouse click and presumably before the revised polygon is createdso it could be before or after the line vertices append(pti arbitrarily chose for it to go before the vertices list is changed the rest of the order of the lines is pretty well fixed by the basic logic if you think of the repetitions through large number of loopsthe process is essentially circular (as suggested by the word 'loop'the body of loop in pythonhoweveris written as linear sequenceone with first line and last linea beginning and an end we can cut circular loop anywhere to get piece with beginning and an end in practicethe place you cut the loop for python has one main constraintthe processing in python from the end of one time through the loop to the beginning of the next loop is separated by the test of the condition in the heading the continuation condition in the while heading must make sense where you cut the loop it can help to look at concrete example sequencelike the steps listed above for creating triangleonly now assuming more on flow of control