Python Forum
is this really a syntax error?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is this really a syntax error?
#1
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>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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.
Reply
#3
so what did it think was a syntax error?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
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.
Reply
#5
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
>>>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  World Clock syntax error OscarBoots 1 335 May-03-2024, 05:20 AM
Last Post: snippsat
  Syntax error for "root = Tk()" dlwaddel 15 1,784 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 561 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,986 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,454 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,496 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,444 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 1,043 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 2,102 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Mysql Syntax error in pymysql ilknurg 4 2,541 May-18-2022, 06:50 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020