12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

196 Appendix A. DebuggingIfyouarecopyingcodefromabook,startbycomparingyourcodetothebook’scodeverycarefully.Check every character. At the same time, remember that the book might be wrong, so if you seesomething that looks likeasyntax error,it might be.Here aresomeways toavoid themost common syntax errors:1. Make sureyou arenot using a<strong>Python</strong> keyword for avariable name.2. Checkthatyouhaveacolonattheendoftheheaderofeverycompound statement,includingfor,while,if,anddefstatements.3. Make surethat any stringsinthe code have matching quotation marks.4. If you have multiline strings with triple quotes (single or double), make sure you have terminatedthe string properly. An unterminated string may cause an invalid token error atthe end of your program, or it may treat the following part of the program as a string until itcomes tothe next string. Inthe second case, itmight not produce an error message at all!5. An unclosed opening operator—(, {, or [—makes <strong>Python</strong> continue with the next line as partof the current statement. Generally, an error occurs almost immediately inthenext line.6. Check for theclassic=instead of==insideaconditional.7. Check the indentation to make sure it lines up the way it is supposed to. <strong>Python</strong> can handlespaceandtabs,butifyoumixthemitcancauseproblems. Thebestwaytoavoidthisproblemistouse atext editor that knows about <strong>Python</strong> and generates consistent indentation.Ifnothing works,move ontothe next section...A.1.1 I keepmakingchangesand itmakesnodifference.If the interpreter says there is an error and you don’t see it, that might be because you and theinterpreter are not looking at the same code. Check your programming environment to make surethat the program you areediting istheone <strong>Python</strong> istryingtorun.Ifyouarenotsure,tryputtinganobviousanddeliberatesyntaxerroratthebeginningoftheprogram.Now runitagain. Ifthe interpreter doesn’t find thenew error,you arenot running thenew code.There areafew likelyculprits:• Youeditedthefileandforgottosavethechangesbeforerunningitagain. Someprogrammingenvironments do thisfor you, but somedon’t.• You changed thename of the file, but you are stillrunning the oldname.• Something inyour development environment is configured incorrectly.• If you are writing a module and using import, make sure you don’t give your module thesame name as one of the standard <strong>Python</strong> modules.• Ifyouareusingimporttoreadamodule,rememberthatyouhavetorestarttheinterpreterorusereloadtoreadamodified file. Ifyou import the module again, itdoesn’t do anything.If you get stuck and you can’t figure out what is going on, one approach is to start again with a newprogram like “Hello, World!,” and make sure you can get a known program to run. Then graduallyadd the pieces ofthe original program tothe new one.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!