Python Programming: An Introduction to Computer Science
6.5.FUNCTIONSTHAT RETURNVALUES 93 Theneteffectis asif thefunctionbodyhadbeenprefacedwiththreeassignmentstatements. window = win ...
94 CHAPTER6.DEFININGFUNCTIONS Program: triangle2.py from graphics import* def square(x): return x * x def distance(p1, p2): dist ...
6.6.FUNCTIONSANDPROGRAMSTRUCTURE 95 Aswithparameters,whenmultiplevaluesarereturnedfroma function,they areassignedtovariablesby p ...
96 CHAPTER6.DEFININGFUNCTIONS Text(Point(-1,0), ’ 0.0K’).draw(win) Text(Point(-1,2500), ’ 2.5K’).draw(win) Text(Point(-1,5000), ...
6.7.EXERCISES 97 futval_graph4.py from graphics import* def createLabeledWindow(): window = GraphWin("InvestmentGrowth Chart", 3 ...
98 CHAPTER6.DEFININGFUNCTIONS Functionscanbethoughtofasminiature(sub)programsinsideofotherprograms. Like any other program,weca ...
6.7.EXERCISES 99 Redoany ofyourfavoriteprogrammingproblemsfrompreviouschaptersandusea functionortwo toencapsulatethecalculation ...
100 CHAPTER6.DEFININGFUNCTIONS ...
Chapter 7 Control Structures, Part 1 Sofar, wehave viewedcomputerprogramsassequencesofinstructionsthatarefollowedoneafterthe oth ...
102 CHAPTER7. CONTROLSTRUCTURES,PART 1 Thisnewdesignhastwo simpledecisionsattheend. Theindentationindicatesthata stepshouldbe pe ...
7.1.SIMPLEDECISIONS 103 Thebodyis justa sequenceofoneormorestatementsindentedundertheifheading.Inconvert2.py therearetwoifstatem ...
104 CHAPTER7. CONTROLSTRUCTURES,PART 1 >>> 3 < 4 1 >>> 3 * 4 < 3 + 4 0 "hello" == "hello" 1 "hello" & ...
7.2.TWO-WAY DECISIONS 105 if name == ’main’: main() Thisguaranteesthatmainwillautomaticallyrunwhentheprogramis invokeddirectly, ...
106 CHAPTER7. CONTROLSTRUCTURES,PART 1 def main(): print "This programfinds the real solutions to a quadratic\n" a, b, c = input ...
7.3.MULTI-WAY DECISIONS 107 no yes Calculate roots Print "no roots" discrim < 0? Figure7.3:Quadraticsolverasa two-waydecision ...
108 CHAPTER7. CONTROLSTRUCTURES,PART 1 quadratic3.main() This program findsthe real solutions to a quadratic Please enter ...
7.4.EXCEPTIONHANDLING 109 discrim < 0? Print "no roots" yes no yes no Do Double Root Do Unique Roots discrim == 0? Figure7.4: ...
110 CHAPTER7. CONTROLSTRUCTURES,PART 1 rarebutpossibleerrors. Inthecaseofthequadraticsolver, wecheckedthedatabeforethecalltothes ...
7.4.EXCEPTIONHANDLING 111 Traceback (innermostlast): File "", line1, in? File "quadratic.py",line 13, in? discRoot = math.sqrt(b ...
112 CHAPTER7. CONTROLSTRUCTURES,PART 1 programs,youmightnotworrytoomuchaboutbadinput;however, professionalqualitysoftwareshouldd ...
«
1
2
3
4
5
6
7
8
9
10
»
Free download pdf