if isinside(ptredbutton)color 'redelif isinside(ptyellowbutton)color 'yellowelif isinside(ptbluebutton)color 'blueelse color 'whitenot only is this exact if statement repeated several timesall the conditions within the if statement are very similarpart of the reason did not put this all in function was the large number of separate variables on further inspectionthe particular variables redbuttonyellowbuttonbluebuttonall play similar roleand their names are not really importantit is their associations that are importantthat redbutton goes with 'red'when there is sequence of things all treated similarlyit suggests list and loop an issue here is that the changing data is paireda rectangle with color string there are number of ways to handle such associations very neat way in python to package pair (or more things togetheris tupleturning several things into one objectas in (redbuttton'red'objects such are this tuple can be put in larger listchoicepairs [(redbuttton'red')(yellowbutton'yellow')(bluebutton'blue')such tuples may be neatly handled in for statement you can imagine function to encapsulate the color choice startingdef getchoice(choicepairsdefaultwin)'''given list choicepairs of tupleswith each tuple in the form (rectanglechoice)return the choice that goes with the rectangle in win where the mouse gets clickedor return default if the click is in none of the rectangles ''point win getmouse(for (rectanglechoicein choicepairsthis is the first time we have had for loop going through list of tuples recall that we can do multiple assignments at once with tuples this also works in for loop heading the for loop goes through one tuple in the list choicepairs at time the first time through the loop the tuple taken from the list is (redbuttton'red'this for loop does multiple assignment to (rectanglechoiceeach time through the loopso the first time rectangle refers to redbutton and choice refers to 'redthe next time through the loopthe second tuple from the list is used(yellowbutton'yellow'so this time inside the loop rectangle will refer to yellowbutton and choice refers to 'yellowthis is neat python feature there is still problem we could test each rectangle in the for-each loopbut the original if-elif statement in choosebutton py stops when the first condition is true however for-each statements are designed to go all the way through the sequence there is simple way out of this in functiona return statement always stops the execution of function when we have found the rectangle containing the pointthe function can return the desired choice immediatelydef getchoice(choicepairsdefaultwin)'''given list of tuples (rectanglechoice)return the choice that goes with the rectangle in win where the mouse gets clickedor return default if the click is in none of the rectangles ''point win getmouse( particularly in other object-oriented languages where lists and tuples are way less easy to usethe preferred way to group associated objectslike rectangle and choiceis to make custom object type containing them all this is also possible and often useful in python in some relatively simple caseslike in the current exampleuse of tuples can be easier to followthough the approach taken is matter of taste the topic of creating custom type of objects will not be taken up in this tutorial loops and tuples