Python Programming: An Introduction to Computer Science
7.5.STUDY INDESIGN:MAXOFTHREE 113 Thesecondquestiontoaskis theconverseofthefirst.Arewecertainthatthisconditionis trueinall cases ...
114 CHAPTER7. CONTROLSTRUCTURES,PART 1 yes no yes no yes no max = x1 max = x3 max = x2 max = x3 x1 >= x3 x2 > =x3 x1 >= ...
7.5.STUDY INDESIGN:MAXOFTHREE 115 max = x1 x2 > max max = x2 max = x3 x3 > max Figure7.6:Flowchartofa sequentialapproachto ...
116 CHAPTER7. CONTROLSTRUCTURES,PART 1 7.5.4 Strategy4:UsePython. Beforeleavingthisproblem,I reallyshouldmentionthatnoneofthealg ...
7.6.EXERCISES 117 a, b, c = input(’Enter three numbers: ’) if a > b: if b > c: print "SpamPlease!" else: print "It’sa late ...
118 CHAPTER7. CONTROLSTRUCTURES,PART 1 Ababysittercharges$2.50anhouruntil9:00PMwhentheratedropsto$1.75anhour(thechildren areinb ...
Chapter 8 Control Structures, Part 2 InChapter7, welookedin detailat thePythonifstatementanditsusein implementingdesignpatternss ...
120 CHAPTER8. CONTROLSTRUCTURES,PART 2 x = input("Entera number >> ") sum = sum + x print "\nThe averageof the numbers is" ...
8.3.COMMONLOOPPATTERNS 121 <body> yes <condition>? no Figure8.1:Flowchartofawhileloop. for i in range(11): print i N ...
122 CHAPTER8. CONTROLSTRUCTURES,PART 2 Recallthatthepreviousversionoftheprogramforcedtheusertocountuphow many numberstherewere t ...
8.3.COMMONLOOPPATTERNS 123 The average of the numbersis 46.5 Inthisversion,theuserdoesn’t have tocountthedatavalues,buttheinterf ...
124 CHAPTER8. CONTROLSTRUCTURES,PART 2 Thissentinelloopsolutionisquitegood,butthereisstilla limitation. Theprogramcan’t beusedto ...
8.3.COMMONLOOPPATTERNS 125 8.3.3 FileLoops Onedisadvantageofalltheaveragingprogramspresentedsofaris thatthey areinteractive. Ima ...
126 CHAPTER8. CONTROLSTRUCTURES,PART 2 line = infile.readline() while line != "": sum = sum + eval(line) count = count+ 1 line = ...
8.4.COMPUTINGWITHBOOLEANS 127 # update sum and countfor values in line for xStr in string.split(line): sum = sum + eval(xStr) co ...
128 CHAPTER8. CONTROLSTRUCTURES,PART 2 P Q PandQ T T T T F F F T F F F F Inthistable,PandQrepresentsmallerBooleanexpressions.Sin ...
8.4.COMPUTINGWITHBOOLEANS 129 scoreA == 15 or scoreB== 15 Wheneitherscorereaches15,oneofthetwo simpleconditionsbecomestrue,and,b ...
130 CHAPTER8. CONTROLSTRUCTURES,PART 2 a or (b and c) == (a or b) and (a or c) a and (b or c) == (a and b) or (a and c) A double ...
8.5.OTHERCOMMONSTRUCTURES 131 Theideahereis thattheloopkeepsgettinginputsuntilthevalueis acceptable.Theflowchartdepictingthis de ...
132 CHAPTER8. CONTROLSTRUCTURES,PART 2 number = -1 # Startwith an illegal value to get into the loop. while number < 0: numbe ...
«
2
3
4
5
6
7
8
9
10
11
»
Free download pdf