8.1 语法错误 Syntax Errors

Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while you are still learning Python:

语法错误,也称作解析错误,可能是学习 Python 的过程中最容易犯的:

>>> while True print 'Hello world'
  File "<stdin>", line 1, in ?
    while True print 'Hello world'
                   ^
SyntaxError: invalid syntax

The parser repeats the offending line and displays a little `arrow' pointing at the earliest point in the line where the error was detected. The error is caused by (or at least detected at) the token preceding the arrow: in the example, the error is detected at the keyword print, since a colon (":") is missing before it. File name and line number are printed so you know where to look in case the input came from a script.

解析器会重复出错的行,并在行中最早发现的错误位置上显示一个小箭头。错误(至少是被检测到的)就发生在箭头指向的位置。示例中的错误表现在关键字 print 上,因为在它之前少了一个冒号( ":" )。同时也会显示文件名和行号,这样你就可以知道错误来自哪个脚本,什么位置。