Python Programming: An Introduction to Computer Science

(Nora) #1
10.6. EXERCISES 173

10.6 Exercises



  1. Explainthesimilaritiesanddifferencesbetweeninstancevariablesand“regular”functionvariables.

  2. 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()


  1. UsetheButtonclassdiscussedinthischaptertobuilda GUIforone(ormore)ofyourprojectsfrom
    previouschapters.

  2. Writea modifiedButtonclassthatcreatescircularbuttons.

  3. Writea shellgameprogramusingseveraldifferentshapesofbuttons.Theprogramshoulddrawat
    leastthreeshapesonthescreenand“pick”oneofthemat random.Theusertriestoguesswhichofthe
    shapesis thespecialone(byclickinginit).Theuserwinsif he/shepickstherightone.

  4. Writea setofclassescorrespondingtothegeometricsolids:cube,rectangularprism(brick),sphere
    andcylinder. Eachclassshouldhave a constructorthatallowsforthecreationofdifferentsizedobjects
    (e.g.,a cubeis specifiedbythelengthofitsside)andmethodsthatreturnthesurfaceareaandvolume
    ofthesolid.

  5. Extendthepreviousproblemtoincludea method,inside, thatdetermineswhethera particularpoint
    lieswithinthesolid.Themethodshouldacceptthreenumbersrepresentingthex,yandzcoordinates
    ofa pointandreturntrueif thepointis insidetheobjectandfalseotherwise.Youmayassumethatthe
    objectsarealwayscenteredat



0  0  0 .


  1. 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
Free download pdf