102 CHAPTER7. CONTROLSTRUCTURES,PART 1
Thisnewdesignhastwo simpledecisionsattheend. Theindentationindicatesthata stepshouldbe
performedonlyif theconditionlistedin thepreviouslineis met.Theideahereis thatthedecisionintroduces
analternative flow ofcontrolthroughtheprogram.Theexactsetofstepstakenbythealgorithmwilldepend
onthevalueoffahrenheit.
Figure7.1is a flowchartshowingthepossiblepathsthatcanbetakenthroughthealgorithm.Thediamond
boxesshow conditionaldecisions.If theconditionis false,controlpassesto thenext statementin thesequence
(theonebelow).If theconditionholds,however, controltransferstotheinstructionsintheboxtotheright.
Oncetheseinstructionsaredone,controlthenpassestothenextstatement.
yes
no
yes
no
fahrenheit < 30?
fahrenheit > 90?
Print a Heat Warning
Print a Cold Warning
Print Fahrenheit
Farenheit = 9/5 * celsius + 32
Input Celsius Temperature
Figure7.1:Flowchartoftemperatureconversionprogramwithwarnings.
Hereis how thenew designtranslatesintoPythoncode:
convert2.py
A program to convertCelsius temps to Fahrenheit.
This versionissues heat and cold warnings.
def main():
celsius = input("Whatis the Celsius temperature? ")
fahrenheit = 9.0 / 5.0 * celsius+ 32
print "The temperatureis", fahrenheit, "degrees fahrenheit."
# Print warningsfor extreme temps
if fahrenheit > 90:
print "It’sreally hot out there, be careful!"
if fahrenheit < 30:
print "Brrrrr.Be sure to dress warmly!"
main()
YoucanseethatthePythonifstatementis usedtoimplementthedecision.
Theformoftheifis verysimilartothepseudo-codeinthealgorithm.
if