InstanceDeveloper
InstanceDeveloper

Reputation: 89

python syntax error in the print command

enter image description here

Hello everyone, I've read in LPTH that python represents it error with ^ an indication at the point of error but when i used capital P in the print command the error indication is on end of the line. Is there any specific reason regarding it. I've posted an image regarding it under this description for a clear understanding.

Upvotes: 0

Views: 1435

Answers (2)

GLHF
GLHF

Reputation: 4054

>>>  Print ("hello")

SyntaxError: unexpected indent

The first one, you wrote, with a space. Before the print() function if you push to space button ,its SyntaxError.

>>> Print ("hello")
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    Print ("hello")
NameError: name 'Print' is not defined
>>> 

Without space button, its NameError because Print function calls with lowercase "p". print().

As you know we starting read documents by left to right, so Python does either. On first case, Python saw the "gap" first then throwed SyntaxError,not NameError and program is crushed. Second one there is no gap so throws NameError.

Edit: Probably you using Python 3x, thats because its a different SyntaxError. You should use parantheses in Python 3x.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799520

What has happened is that in order to be able to parse structures that don't use parens, e.g. print, if, etc., the Python parser has to be a bit liberal in what it parses. It lexes an additional bit of the line beyond the first part and only then does it parse what it has read. Since "Print" does not match anything it should parse, only then does it report a syntax error. This can be shown if we add more to the line:

>>> Print "foo" "bar"
  File "<stdin>", line 1
    Print "foo" "bar"
              ^
SyntaxError: invalid syntax

Upvotes: 2

Related Questions