Hardware Reference
In-Depth Information
SYNTAX, ERRORS AND DEBUGGING
YOUR CODE
Syntax is a set of rules to check whether the code you have typed is valid
Python code. In the same way as the English language has rules about how to
properly combine subjects, verbs, objects and so on, each programming lan-
guage has its own syntax. When you make a mistake or a typo in your code,
your program may display a syntax error message.
A syntax error stops a program from running because the computer cannot
understand the code. This usually happens because a word was misspelled or a
character left out. The most common cause of syntax errors is missing out the
colon at the end of loops and conditionals!
Error messages are posted to the screen to alert you to the problem, but these
messages can be dificult to understand. You might want to make some typing
mistakes on purpose with some simple example code, so you can see the sort
of error messages Python gives you. Try leaving out a quote mark or bracket, or
misspelling a command, to ind out what happens.
So what do you do when you get an error message? Debugging is the act of
locating the cause of any errors in your computer program code and ixing
them. When Python displays a syntax error, the line that contains the error is
repeated, with a little arrow underneath it pointing to where the error is likely to
originate. Look carefully at the line to spot any misspelled words or missing
characters, then correct the problem and try running the code again.
FIGURE 5-2 he Python 3 IDLE Shell
 
Search WWH ::




Custom Search