0

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.

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0
>>>  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.

2 Comments

Hi qqvc, i haven't written with any space, but if i mention the same command with a parenthesis then it shows me your second form of representation. but if you try without any parenthesis then the o/p is error indication at the eol.
No sir, I'm using python 2.7.9 version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.