Python Programming: An Introduction to Computer Science

(Nora) #1
5.8.EXERCISES 83


  1. Anarcherytargetconsistsofa centralcircleofyellowsurroundedbyconcentricringsofred,blue,
    blackandwhite.Eachringhasthesame“width,” whichis thesameastheradiusoftheyellow circle.
    Writea programthatdrawssucha target.Hint:Objectsdrawnlaterwillappearontopofobjectsdrawn
    earlier.

  2. Writea programthatdrawssomesortofface.

  3. Writea programthatdrawsa winterscenewitha Christmastreeanda snowman.

  4. Writea programthatdraws5 diceonthescreendepictinga straight(1,2, 3, 4, 5 or2, 3, 4, 5, 6).

  5. Modifythegraphicalfuturevalueprogramsothattheinput(principalandapr)alsoaredoneina
    graphicalfashionusingEntryobjects.

  6. CircleIntersection.Writea programthatcomputestheintersectionofa circlewitha horizontalline
    anddisplaystheinformationtextuallyandgraphically.


Input:Radiusofthecircleandthey-interceptoftheline.
Output:Draw a circlecenteredat


0  0  withthegivenradiusina window withcoordinatesrunning
from-10,-10to10,10.
Draw a horizontallineacrossthewindow withthegiveny-intercept.
Draw thetwo pointsofintersectioninred.
Printoutthexvaluesofthepointsofintersection.

Formula:x


 r^2  y^2


  1. LineInformation.
    Thisprogramallowstheusertodrawa linesegmentandthendisplayssomegraphicalandtextual
    informationaboutthelinesegment.


Input:2 mouseclicksfortheendpointsofthelinesegment.
Output:Draw themidpointofthesegmentincyan.
Draw theline.
Printthelengthandtheslopeoftheline.
Formulas: dx x 2  x 1
dy y 2  y 1
slo pe dy


dx
length  dx^2  dy^2


  1. RectangleInformation.
    Thisprogramdisplaysinformationabouta rectangledrawnbytheuser.


Input:2 mouseclicksfortheoppositecornersofa rectangle.
Output:Draw therectangle.
Printtheperimeterandareaoftherectangle.

Formulas:

area


length


width
perimeter 2


length width


  1. TriangleInformation.
    Sameaspreviousproblem,butwith3 clicksfortheverticiesofa triangle.


Formulas:Forperimeter, seelengthfromlineproblem.
area  s


s a


s b


s cwhereabandcarethelengthsofthesidesands a

b c
2
Free download pdf