![]() |
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?
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 >>> |