Python Forum
is this really a syntax error? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: is this really a syntax error? (/thread-42191.html)



is this really a syntax error? - Skaperen - May-24-2024

if i run this from a file it works as expected. if i type the same code into interactive python, i get a syntax error. which is the mistake? is this really a syntax error?

Output:
lt1a/forums/2 /home/forums 5> cat -n this.py 1 foo = [] 2 for x in foo: 3 print('hi') 4 print('bye') lt1a/forums/2 /home/forums 6> python3 this.py bye lt1a/forums/2 /home/forums 7> python3 Python 3.8.10 (default, Nov 22 2023, 10:22:35) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> foo = [] >>> for x in foo: ... print('hi') ... print('bye') File "<stdin>", line 3 print('bye') ^ SyntaxError: invalid syntax >>> print('bar') bar >>> lt1a/forums/2 /home/forums 8>



RE: is this really a syntax error? - bowlofred - May-24-2024

In the interactive interpreter, if you enter a command that requires continuation information (like an if or a loop), the prompt changes to the secondary prompt ....

You have to exit that mode (by entering a blank line) before entering a new command.


RE: is this really a syntax error? - Skaperen - May-24-2024

so what did it think was a syntax error?


RE: is this really a syntax error? - deanhystad - May-25-2024

I expected an indentation error, but a little experiment shows you only get that for the line immediately following for/if. Indentation errors for the second line are reported as syntax errors.

For the interactive interpreter it is a syntax error. As mentioned by bowlofred, the ... prompt tells you that you are in the body following the for statement. Enter an empty line to get the >>> prompt.


RE: is this really a syntax error? - snippsat - May-25-2024

If use a better interactive interpreter like eg Ipython or ptpython(my favorite) it will work.
foo = []
for x in foo:
    print('hi')
print('bye')
Same copy of code over into the 3 interactive interpreter.
C:\code
λ ipython
Python 3.12.2 (tags/v3.12.2:6abddd9, Feb  6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.24.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: foo = []
   ...: for x in foo:
   ...:     print('hi')
   ...: print('bye')
bye
In [2]: exit()

C:\code
λ ptpython
>>> foo = []
... for x in foo:
...     print('hi')
... print('bye')
bye
>>> exit()

# With the stander one it failes
C:\code
λ python
Python 3.12.2 (tags/v3.12.2:6abddd9, Feb  6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> foo = []
>>> for x in foo:
...     print('hi')
... print('bye')
  File "<stdin>", line 3
    print('bye')
    ^^^^^
SyntaxError: invalid syntax
>>>