Python Programming: An Introduction to Computer Science

(Nora) #1

Chapter 2


Writing Simple Programs


Asyousaw inthepreviouschapter, it is easyto runprogramsthathave alreadybeenwritten.Thehardpartis
actuallycomingupwiththeprograminthefirstplace.Computersareveryliteral,andthey mustbetoldwhat
todorightdowntothelastdetail.Writinglargeprogramsis a dauntingtask.It wouldbealmostimpossible
withouta systematicapproach.


2.1 TheSoftwareDevelopmentProcess


Theprocessofcreatinga programisoftenbrokendownintostagesaccordingtotheinformationthatis
producedineachphase.Ina nutshell,here’s whatyoushoulddo.


FormulateRequirementsFigureoutexactlywhattheproblemtobesolvedis.Trytounderstandasmuch
aspossibleaboutit.Untilyoureallyknow whattheproblemis,youcannotbegintosolve it.


DetermineSpecificationsDescribeexactlywhatyourprogramwilldo.Atthispoint,youshouldnotworry
abouthowyourprogramwillwork,butratherwithdecidingexactlywhatit willaccomplish. For
simpleprogramsthisinvolvescarefullydescribingwhattheinputsandoutputsoftheprogramwillbe
andhow they relatetoeachother.


Createa DesignFormulatetheoverallstructureoftheprogram.Thisis wherethehowoftheprogramgets
workedout.Themaintaskis todesignthealgorithm(s)thatwillmeetthespecifications.


ImplementtheDesignTranslatethedesignintoa computerlanguageandputit intothecomputer. Inthis
book,wewillbeimplementingouralgorithmsasPythonprograms.


Test/DebugtheProgramTryoutyourprogramandseeif it worksasexpected.If thereareany errors(often
calledbugs), thenyoushouldgobackandfixthem.Theprocessoflocatingandfixingerrorsis called
debugginga program.


MaintaintheProgramContinuedevelopingtheprograminresponsetotheneedsofyourusers. Most
programsareneverreallyfinished;they keepevolvingoveryearsofuse.


2.2 ExampleProgram:TemperatureConverter.


Let’s gothroughthestepsofthesoftwaredevelopmentprocesswitha simplereal-worldexample. Suzie
Programmerhasa problem. SuzieisanAmericancomputersciencestudentspendinga yearstudyingin
Europe. Shehasnoproblemswithlanguage,assheis fluentinmany languages(includingPython). Her
problemis thatshehasa hardtimefiguringoutthetemperatureinthemorningsothatsheknowshowto
dressfortheday. Suzielistenstotheweatherreporteachmorning,butthetemperaturesaregivenindegrees
Celsius,andsheis usedtoFahrenheit.


13
Free download pdf