10.6. EXERCISES 173
10.6 Exercises
- Explainthesimilaritiesanddifferencesbetweeninstancevariablesand“regular”functionvariables.
- Show theoutputthatwouldresultfromthefollowingnonsenseprogram.
class Bozo:
def __init__(self,value):
print "Creatinga Bozo from:", value
self.value= 2 * value
def clown(self,x):
print "Clowning:",x
print x * self.value
return x + self.value
def main():
print "Clowningaround now."
c1 = Bozo(3)
c2 = Bozo(4)
print c1.clown(3)
print c2.clown(c1.clown(2))
main()
- UsetheButtonclassdiscussedinthischaptertobuilda GUIforone(ormore)ofyourprojectsfrom
previouschapters.
- Writea modifiedButtonclassthatcreatescircularbuttons.
- Writea shellgameprogramusingseveraldifferentshapesofbuttons.Theprogramshoulddrawat
leastthreeshapesonthescreenand“pick”oneofthemat random.Theusertriestoguesswhichofthe
shapesis thespecialone(byclickinginit).Theuserwinsif he/shepickstherightone.
- Writea setofclassescorrespondingtothegeometricsolids:cube,rectangularprism(brick),sphere
andcylinder. Eachclassshouldhave a constructorthatallowsforthecreationofdifferentsizedobjects
(e.g.,a cubeis specifiedbythelengthofitsside)andmethodsthatreturnthesurfaceareaandvolume
ofthesolid.
- Extendthepreviousproblemtoincludea method,inside, thatdetermineswhethera particularpoint
lieswithinthesolid.Themethodshouldacceptthreenumbersrepresentingthex,yandzcoordinates
ofa pointandreturntrueif thepointis insidetheobjectandfalseotherwise.Youmayassumethatthe
objectsarealwayscenteredat
0 0 0 .
- Hereis a simpleclassthatdrawsa (grim)faceina graphicswindow.
# face.py
from graphics import*
class Face:
def __init__(self,window, center, size):
eyeSize = 0.15* size
eyeOff = size/ 3.0
mouthSize= 0.8 * size