Python Programming: An Introduction to Computer Science

(Nora) #1
118 CHAPTER7. CONTROLSTRUCTURES,PART 1


  1. Ababysittercharges$2.50anhouruntil9:00PMwhentheratedropsto$1.75anhour(thechildren
    areinbed). Writea programthatacceptsa startingtimeandendingtimeinhoursandminutesand
    calculatesthetotalbabysittingbill.Youmayassumethatthestartingandendingtimesareina single
    24 hourperiod.Partialhoursshouldbeappropriatelyprorated.

  2. A personis eligibletobea USsenatorif they areat least 30 yearsoldandhave beena UScitizenfor
    at least9 years.To bea USrepresentative thesenumbersare 25 and7,respectively. Writea program
    thatacceptsa person’s ageandyearsofcitizenshipasinputandoutputstheireligibilityfortheSenate
    andHouse.

  3. AformulaforcomputingEasterintheyears1982–2048,inclusive,isasfollows:leta year%19,
    b year%4,c year%7,d



19 a 24 %30,e


2 b 4 c 6 d 5 %7.ThedateofEasteris March
22  d e(whichcouldbeinApril).Writea programthatinputsa year, verifiesthatit is intheproper
rangeandthenprintsoutthedateofEasterthatyear.


  1. TheformulaforEasterinthepreviousproblemworksforeveryyearintherange1900–2099except
    for1954,1981, 2049 and2076.Forthese4 yearsit producesa datethatis oneweektoolate.Modify
    theabove programtoworkfortheentirerange1900–2099.

  2. A yearis a leapyearif it is divisibleby4, unlessit is a centuryyearthatis notdivisibleby400.(1800
    and 1900 arenotleapyearswhile 1600 and 2000 are.) Writea programthatcalculateswhethera year
    is a leapyear.

  3. Writea programthatacceptsa dateintheformmonth/day/yearandoutputswhetherornotthedateis
    valid.Forexample5/24/1962is valid,but9/31/2000is not.(Septemberhasonly 30 days.)

  4. Thedaysoftheyearareoftennumberedfrom1 to through 365 (or366).Thisnumbercanbecomputed
    inthreestepsusingintarithmetic:


(a)dayNum 31


month 1  day
(b)if themonthis afterFebruarysubtract


4 month 23 


10
(c)if it’s a leapyearandafterFebruary29,add 1

Writea programthatacceptsa dateasmonth/day/year, verifiesthatit isa validdate(seeprevious
problem)andthencalculatesthecorrespondingdaynumber.


  1. Take a favoriteprogrammingproblemfroma previouschapterandadddecisionsand/orexception
    handlingasrequiredtomake it trulyrobust(willnotcrashonany inputs).

  2. ArcheryScorer. Writea programthatdrawsanarcherytarget(seeexercisesfromChapter5)and
    allowstheusertoclick5 timestorepresentarrowsshotat thetarget.Using5-bandscoring,a bulls-eye
    (yellow)is worth9 pointsandeachsuccessive ringis worth2 fewerpointsdownto1 forwhite.The
    programshouldoutputa scoreforeachclickandkeeptrackofa runningsumfortheentireseries.

Free download pdf