14 CHAPTER2. WRITINGSIMPLEPROGRAMS
Fortunately, Suziehasanideatosolve theproblem. Beinga computersciencemajor, shenevergoes
anywherewithoutherlaptopcomputer. Shethinksit mightbepossiblethata computerprogramcouldhelp
herout.
Suziebeginswiththerequirementsofherproblem.Inthiscase,theproblemis prettyclear:theradioan-
nouncergivestemperaturesindegreesCelsius,butSuzieonlycomprehendstemperaturesthatareindegrees
Fahrenheit.That’s thecruxoftheproblem.
Next,Suzieconsidersthespecificationsofa programthatmighthelpherout. Whatshouldtheinput
be? ShedecidesthatherprogramwillallowhertotypeinthetemperatureindegreesCelsius. Andthe
output? TheprogramwilldisplaythetemperatureconvertedintodegreesFahrenheit. Nowsheneedsto
specifytheexactrelationshipoftheoutputtotheinput.Suziedoessomequickfiguringtoderive theformula
F
9
5 C 32 (Canyouseehow?).Thatseemsanadequatespecification.
Noticethatthisdescribesoneofmany possibleprogramsthatcouldsolve thisproblem. IfSuziehad
backgroundinthefieldofArtificialIntelligence(AI),shemightconsiderwritinga programthatwould
actuallylistentotheradioannouncertogetthecurrenttemperatureusingspeechrecognitionalgorithms.For
output,shemighthave thecomputercontrola robotthatgoestoherclosetandpicksanappropriateoutfit
basedontheconvertedtemperature.Thiswouldbea muchmoreambitiousproject,tosaytheleast!
Certainlytherobotprogramwouldalsosolve theproblemidentifiedintherequirements.Thepurposeof
specificationis todecideexactlywhatthisparticularprogramwilldotosolve a problem.Suzieknowsbetter
thantojustdive inandstartwritinga programwithoutfirsthavinga clearideaofwhatsheis tryingtobuild.
Suzieis now readytodesignanalgorithmforherproblem.Sheimmediatelyrealizesthatthisis a simple
algorithmthatfollowsa standardpattern:Input,Process,Output(IPO). Herprogramwillprompttheuser
forsomeinputinformation(theCelsiustemperature),processit toconverttoa Fahrenheittemperature,and
thenoutputtheresultbydisplayingit onthecomputerscreen.
Suziecouldwriteheralgorithmdownina computerlanguage.However, theprecisionofwritingit out
formallytendstostiflethecreative processofdevelopingthealgorithm.Instead,shewritesheralgorithm
usingpseudocode. Pseudocodeis justpreciseEnglishthatdescribeswhata programdoes. It is meantto
communicatealgorithmswithoutalltheextramentaloverheadofgettingthedetailsrightinany particular
programminglanguage.
Hereis Suzie’s completedalgorithm:
Input the temperaturein degrees Celsius (call it celsius)
Calculate fahrenheitas 9/5 celsius + 32
Output fahrenheit
Thenextstepis totranslatethisdesignintoa Pythonprogram.Thisis straightforward,aseachlineofthe
algorithmturnsintoa correspondinglineofPythoncode.
convert.py
A program to convertCelsius temps to Fahrenheit
by: Suzie Programmer
def main():
celsius = input("Whatis the Celsius temperature? ")
fahrenheit = 9.0 / 5.0 * celsius+ 32
print "The temperatureis", fahrenheit, "degrees Fahrenheit."
main()
Seeif youcanfigureoutwhateachlineofthisprogramdoes.Don’t worryif somepartsarea bitconfusing.
They willbediscussedindetailinthenextsection.
Aftercompletingherprogram,Suzietestsit toseehowwellit works.Sheusessomeinputsforwhich
sheknowsthecorrectanswers.Hereis theoutputfromtwo ofhertests.
What is the Celsiustemperature? 0
The temperature is 32.0degrees fahrenheit.