Python Programming: An Introduction to Computer Science

(Nora) #1
7.6.EXERCISES 117

a, b, c = input(’Enter three numbers: ’)
if a > b:
if b > c:
print "SpamPlease!"
else:
print "It’sa late parrot!"
elif b > c:
print "CheeseShoppe"
if a >= c:
print "Cheddar"
elif a < b:
print "Gouda"
elif c == b:
print "Swiss"
else:
print "Trees"
if a == b:
print "Chestnut"
else:
print "Larch"
print "Done"

Show theoutputthatwouldresultfromeachofthefollowingpossibleinputs.

(a)3, 4, 5
(b)3, 3, 3
(c)5, 4, 3
(d)3, 5, 2
(e)5, 4, 7
(f)3, 3, 2


  1. Many companiespaytime-and-a-halfforany hoursworkedabove 40in a givenweek.Writea program
    toinputthenumberofhoursworkedandthehourlyrateandcalculatethetotalwagesfortheweek.

  2. A certainCSprofessorgives5-pointquizzesthataregradedonthescale5-A,4-B,3-C,2-D,1-F, 0-F.
    Writea programthatacceptsa quizscoreasaninputandusesa decisionstructuretocalculatethe
    correspondinggrade.

  3. AcertainCSprofessorgives100-pointexamsthataregradedonthescale90–100:A,80–89:B,70–
    79:C,60–69:D, 60:F. Writea programthatacceptsanexamscoreasinputandusesa decisionstruc-
    turetocalculatethecorrespondinggrade.

  4. A certaincollegeclassifiesstudentsaccordingtocreditsearned.A studentwithlessthan7 creditsis a
    Freshman.Atleast7 creditsarerequiredtobea Sophomore, 16 tobea Juniorand 26 tobeclassified
    asa Senior. Writea programthatcalculatesclassstandingfromthenumberofcreditsearned.

  5. Thebodymassindex (BMI)is calculatedasa person’s weight(inpounds)times720,dividedbythe
    squareoftheperson’s height(ininches).A BMIintherange19–25,inclusive, is consideredhealthy.
    Writea programthatcalculatesa person’s BMIandprintsa messagetellingwhetherthey areabove,
    withinorbelow thehealthyrange.

  6. Thespeedingticket finepolicy in Podunksvilleis $50plus$5foreachmphover thelimitplusa penalty
    of$200forany speedover 90 mph.Writea programthatacceptsa speedlimitanda clockedspeed
    andeitherprintsa messageindicatingthespeedwaslegalorprintstheamountofthefine,if thespeed
    is illegal.

Free download pdf