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.

2.10. Debugging 152.10 DebuggingAtthispointthesyntaxerroryouaremostlikelytomakeisanillegalvariablename,likeclassandyield,which are keywords, orodd˜jobandUS$,which contain illegal characters.Ifyou put aspace inavariable name, <strong>Python</strong> thinks itistwooperands without anoperator:>>> bad name = 5SyntaxError: invalid syntaxFor syntax errors, the error messages don’t help much. The most common messages areSyntaxError: invalid syntaxandSyntaxError: invalid token,neitherofwhichisveryinformative.The runtime error you are most likely to make is a “use before def;” that is, trying to use a variablebefore you have assigned avalue. This can happen ifyou spell avariable name wrong:>>> principal = 327.68>>> interest = principle * rateNameError: name 'principle' is not definedVariables names arecase sensitive, soLaTeXisnot thesame aslatex.At this point the most likely cause of a semantic error is the order of operations. For example, toevaluate2π 1 ,you might betempted towrite>>> 1.0 / 2.0 * piBut the division happens first, so you would get π/2, which is not the same thing! There is no wayfor<strong>Python</strong>toknowwhatyoumeanttowrite,sointhiscaseyoudon’tgetanerrormessage;youjustget the wrong answer.2.11 Glossaryvalue: One ofthe basicunits of data, likeanumber or string,that a program manipulates.type: A category of values. The types we have seen so far are integers (type int), floating-pointnumbers (typefloat),and strings(typestr).integer: A type that represents whole numbers.floating-point: A type that represents numbers withfractional parts.string: A type that represents sequences of characters.variable: A name that referstoavalue.statement: A section of code that represents a command or action. So far, the statements we haveseen are assignments and print statements.assignment: A statement that assigns avalue toavariable.statediagram: A graphical representation of aset ofvariables and thevalues they referto.keyword: A reserved word that is used by the compiler to parse a program; you cannot use keywordslikeif,def,andwhileas variable names.

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

Saved successfully!

Ooh no, something went wrong!