Page 40 -
P. 40

2.10. Debugging                                                              17

                           2.10    Debugging

                           At this point the syntax error you are most likely to make is an illegal variable name, like
                           class and yield , which are keywords, or odd~job and US$, which contain illegal charac-
                           ters.

                           If you put a space in a variable name, Python thinks it is two operands without an operator:
                           >>> bad name = 5
                           SyntaxError: invalid syntax
                           For syntax errors, the error messages don’t help much. The most common messages are
                           SyntaxError:  invalid syntax  and SyntaxError:  invalid token  , neither of which is
                           very informative.

                           The runtime error you are most likely to make is a “use before def;” that is, trying to use
                           a variable before you have assigned a value. This can happen if you spell a variable name
                           wrong:
                           >>> principal = 327.68
                           >>> interest = principle * rate
                           NameError: name  'principle ' is not defined
                           Variables names are case sensitive, so LaTeX is not the same as latex .

                           At this point the most likely cause of a semantic error is the order of operations. For exam-
                           ple, to evaluate  1  , you might be tempted to write
                                         2π
                           >>> 1.0 / 2.0 * pi
                           But the division happens first, so you would get π/2, which is not the same thing! There is
                           no way for Python to know what you meant to write, so in this case you don’t get an error
                           message; you just get the wrong answer.



                           2.11 Glossary

                           value: One of the basic units of data, like a number or string, that a program manipulates.


                           type: A category of values. The types we have seen so far are integers (type int), floating-
                                point numbers (type float ), and strings (type str).
                           integer: A type that represents whole numbers.

                           floating-point: A type that represents numbers with fractional parts.
                           string: A type that represents sequences of characters.

                           variable: A name that refers to a value.
                           statement: A section of code that represents a command or action. So far, the statements
                                we have seen are assignments and print statements.
                           assignment: A statement that assigns a value to a variable.

                           state diagram: A graphical representation of a set of variables and the values they refer to.
   35   36   37   38   39   40   41   42   43   44   45