202 Appendix A. DebuggingA.3.5 No,Ireally needhelp.It happens. Even the best programmers occasionally get stuck. Sometimes you work on a programsolong that you can’t seetheerror. A freshpair of eyes isjustthe thing.Before you bring someone else in, make sure you are prepared. Your program should be as simpleas possible, and you should be working on the smallest input that causes the error. You should haveprintstatementsintheappropriateplaces(andtheoutputtheyproduceshouldbecomprehensible).You should understand the problem well enough todescribe it concisely.When you bring someone intohelp, be suretogive them the informationthey need:• Ifthere isan errormessage, what isitand what part of theprogram does itindicate?• What was the last thing you did before this error occurred? What were the last lines of codethat you wrote, or what isthe new testcase that fails?• What have you triedsofar,and what have you learned?Whenyoufindthebug,takeasecondtothinkaboutwhatyoucouldhavedonetofinditfaster. Nexttimeyou seesomething similar,you willbe able tofind thebug morequickly.Remember, the goal is not just to make the program work. The goal is to learn how to make theprogram work.
Indexabecedarian, 72, 83abs function, 52absolute path, 137, 143access, 89accumulator, 100histogram, 127list,93string,173sum, 93Ackerman function, 61add method, 163addition withcarrying, 68algorithm, 3, 7, 67, 130Euclid, 62MD5, 143RSA, 112square root, 69aliasing, 96, 100, 147, 149, 167copying toavoid, 99alphabet, 37alternative execution, 41ambiguity, 5anagram, 100anagram set,123, 140and operator, 40anydbm module, 139append method, 92, 98, 101, 172, 173arcfunction, 31argument, 17, 19, 21, 22, 26, 97gather, 117keyword, 33, 36, 121, 182list,97optional, 76, 95, 107variable-length tuple, 117argument scatter,117arithmeticoperator, 12assertstatement, 157assignment, 15, 63, 89augmented, 93, 100item, 74, 90, 116multiple, 68, 110tuple, 116–118, 123assignment statement, 10attributedict ,166class, 170, 177initializing, 166instance, 146, 151, 170, 177AttributeError,150, 199augmented assignment, 93, 100Austin, Jane, 127available colors, 152, 168Bacon, Kevin, 144Bangladesh, national flag, 152base case, 44, 47benchmarking, 132, 134big, hairyexpression, 200binary search, 101binding, 189, 192bingo, 124birthday, 158birthday paradox, 100bisect module, 101bisection search, 101bisection, debugging by, 68bitwiseoperator, 12body, 19, 25, 65bool type, 39boolean expression, 39, 46boolean function, 54, 153boolean operator, 76borrowing, subtraction with,68, 157bound method, 187, 192bounding box, 152, 184, 192bracketsquiggly, 103bracket operator, 71, 89, 116branch, 41, 47break statement, 65bug, 3, 7worst,167
- Page 1:
Think PythonHowto Think LikeaComput
- Page 4 and 5:
Copyright©2008 AllenDowney.Printin
- Page 6 and 7:
viChapter 0. PrefaceIn 2003 I start
- Page 8 and 9:
viiiChapter 0. Preface• Keith Ver
- Page 10 and 11:
xChapter 0. Preface• Patryk Wolow
- Page 12 and 13:
xiiContents3 Functions 173.1 Functi
- Page 14 and 15:
xivContents7 Iteration 637.1 Multip
- Page 16 and 17:
xviContents12 Tuples 11512.1 Tuples
- Page 18 and 19:
xviiiContents17 Classes and methods
- Page 20 and 21:
xxContents
- Page 22:
2 Chapter 1. The way of theprogramp
- Page 25 and 26:
1.4. Formal and natural languages 5
- Page 27 and 28:
1.7. Glossary 7Your job is to be a
- Page 29 and 30:
Chapter 2Variables,expressions ands
- Page 31 and 32:
2.3. Variable names and keywords 11
- Page 33 and 34:
2.6. Expressions 132.6 ExpressionsA
- Page 35 and 36:
2.10. Debugging 152.10 DebuggingAtt
- Page 37 and 38:
Chapter 3Functions3.1 Functioncalls
- Page 39 and 40:
3.4. Composition 19>>> math.sqrt(2)
- Page 41 and 42:
3.7. Flow of execution 21def repeat
- Page 43 and 44:
3.10. Stack diagrams 23Thisfunction
- Page 45 and 46:
3.12. Why functions? 253.12 Whyfunc
- Page 47 and 48:
3.15. Exercises 27Here’s an examp
- Page 49 and 50:
Chapter 4Case study: interfacedesig
- Page 51 and 52:
4.3. Exercises 31The syntax of a fo
- Page 53 and 54:
4.6. Interface design 33This draws
- Page 55 and 56:
4.8. A development plan 354.8 Adeve
- Page 57 and 58:
4.12. Exercises 37Exercise 4.2 Writ
- Page 59 and 60:
Chapter 5Conditionals and recursion
- Page 61 and 62:
5.5. Alternative execution 41if sta
- Page 63 and 64:
5.9. Stack diagrams for recursive f
- Page 65 and 66:
5.11. Keyboard input 455.11 Keyboar
- Page 67 and 68:
5.14. Exercises 47relational operat
- Page 69 and 70:
5.14. Exercises 492. Write a functi
- Page 71 and 72:
Chapter 6Fruitfulfunctions6.1 Retur
- Page 73 and 74:
6.2. Incremental development 53>>>
- Page 75 and 76:
6.5. More recursion 55It is common
- Page 77 and 78:
6.6. Leap of faith 57__main__factor
- Page 79 and 80:
6.9. Debugging 59The first base cas
- Page 81 and 82:
6.11. Exercises 61sum = x + y + zpo
- Page 83 and 84:
Chapter 7Iteration7.1 Multipleassig
- Page 85 and 86:
7.4. break 652. Iftheconditionisfal
- Page 87 and 88:
7.6. Algorithms 672.00001024003>>>
- Page 89 and 90:
7.9. Exercises 69decrement: An upda
- Page 91 and 92:
Chapter 8Strings8.1 Astringis a seq
- Page 93 and 94:
8.4. String slices 73Thefollowingex
- Page 95 and 96:
8.7. Looping and counting 75This is
- Page 97 and 98:
8.10. String comparison 778.10 Stri
- Page 99 and 100:
8.12. Glossary 798.12 Glossaryobjec
- Page 101 and 102:
Chapter 9Case study: word play9.1 R
- Page 103 and 104:
9.3. Search 83Exercise 9.6 Write a
- Page 105 and 106:
9.5. Debugging 85Here is a version
- Page 107 and 108:
9.7. Exercises 87Write a Python pro
- Page 109 and 110:
Chapter 10Lists10.1 Alist isasequen
- Page 111 and 112:
10.3. Traversing alist 9110.3 Trave
- Page 113 and 114:
10.7. Map, filter and reduce 93List
- Page 115 and 116:
10.9. Listsand strings 95>>> t = ['
- Page 117 and 118:
10.12. Listarguments 97The statedia
- Page 119 and 120:
10.14. Glossary 99word = word.strip
- Page 121 and 122:
10.15. Exercises 101Exercise 10.6 W
- Page 123 and 124:
Chapter 11DictionariesAdictionaryis
- Page 125 and 126:
11.1. Dictionary as asetof counters
- Page 127 and 128:
11.4. Dictionaries and lists 107Thi
- Page 129 and 130:
11.5. Memos 109corresponding locati
- Page 131 and 132:
11.7. Long integers 111count = 0def
- Page 133 and 134:
11.10. Exercises 113implementation:
- Page 135 and 136:
Chapter 12Tuples12.1 Tuples areimmu
- Page 137 and 138:
12.3. Tuples as return values 117>>
- Page 139 and 140:
12.6. Dictionaries and tuples 119If
- Page 141 and 142:
12.7. Comparing tuples 12112.7 Comp
- Page 143 and 144:
12.10. Glossary 123>>> t3 = [1, 2,
- Page 145 and 146:
Chapter 13Case study: data structur
- Page 147 and 148:
13.3. Word histogram 12713.3 Word h
- Page 149 and 150:
13.6. Dictionary subtraction 129num
- Page 151 and 152:
13.9. Datastructures 131In this tex
- Page 153 and 154:
13.11. Glossary 133reading: Examine
- Page 155 and 156:
Chapter 14Files14.1 PersistenceMost
- Page 157 and 158:
14.4. Filenames and paths 137>>> '%
- Page 159 and 160:
14.6. Databases 139Python starts by
- Page 161 and 162:
14.9. Writingmodules 141The argumen
- Page 163 and 164:
14.11. Glossary 14314.11 Glossarype
- Page 165 and 166:
Chapter 15Classes and objects15.1 U
- Page 167 and 168:
15.3. Rectangles 147>>> print '(%g,
- Page 169 and 170:
15.6. Copying 149>>> print box.widt
- Page 171 and 172: 15.8. Glossary 151>>> hasattr(p, 'x
- Page 173 and 174: Chapter 16Classes and functions16.1
- Page 175 and 176: 16.3. Modifiers 155if sum.second >=
- Page 177 and 178: 16.5. Debugging 157But if we have t
- Page 179 and 180: Chapter 17Classes and methods17.1 O
- Page 181 and 182: 17.3. Another example 161Inside the
- Page 183 and 184: 17.6. The str method 163>>> time =
- Page 185 and 186: 17.9. Polymorphism 165Unfortunately
- Page 187 and 188: 17.12. Exercises 167object-oriented
- Page 189 and 190: Chapter 18InheritanceIn this chapte
- Page 191 and 192: 18.3. Comparing cards 171Thefirstel
- Page 193 and 194: 18.6. Add, remove, shuffle and sort
- Page 195 and 196: 18.8. Classdiagrams 175So when you
- Page 197 and 198: 18.10. Glossary 177might get the on
- Page 199 and 200: 18.11. Exercises 1796. Print a tabl
- Page 201 and 202: Chapter 19Case study: Tkinter19.1 G
- Page 203 and 204: 19.3. Canvas widgets 183Exercise 19
- Page 205 and 206: 19.6. Packing widgets 185>>> text.i
- Page 207 and 208: 19.6. Packing widgets 187self.bu(te
- Page 209 and 210: 19.8. Binding 18919.8 BindingAbindi
- Page 211 and 212: 19.9. Debugging 19119.9 DebuggingOn
- Page 213 and 214: 19.11. Exercises 193image = PIL.ope
- Page 215 and 216: Appendix ADebuggingDifferentkindsof
- Page 217 and 218: A.2. Runtime errors 197A.2 Runtime
- Page 219 and 220: A.3. Semantic errors 199• You are
- Page 221: A.3. Semantic errors 201The explici
- Page 225 and 226: Index 205experimental, 4superstitio
- Page 227 and 228: Index 207raw input, 45recursive, 43
- Page 229 and 230: Index 209map pattern, 93, 100map to
- Page 231 and 232: Index 211poker, 169, 178polygon fun
- Page 233 and 234: Index 213knowing the answer, 53leap