Python Forum
for / else not working in interactive mode
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
for / else not working in interactive mode
#1
this little test script works:
Output:
lt2a/phil /home/phil 137> cat try.py #!/usr/bin/env python3 a=[1,2,3,4,5,6] b=[0,5,10] for x in b: if x in a: print('foo') break else: print('bar') print('xyzzy') lt2a/phil /home/phil 138> python3 try.py foo xyzzy lt2a/phil /home/phil 139>
however, doing the same exact thing interactively gets a syntax error:
Output:
lt2a/phil /home/phil 139> python3 Python 3.6.8 (default, Jan 14 2019, 11:02:34) [GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux Type "help", "copyright", "credits" or "license" for more information. >>> a=[1,2,3,4,5,6] >>> b=[0,5,10] >>> for x in b: ... if x in a: ... print('foo') ... break ... else: ... print('bar') ... print('xyzzy') File "<stdin>", line 7 print('xyzzy') ^ SyntaxError: invalid syntax >>>
any idea what is wrong?
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
The interactive interpreter wants you to end the for loop with a blank line before continuing on with more code:

Output:
>>> for i in range(5): ... print(i) ... print(0) File "<stdin>", line 3 print(0) ^ SyntaxError: invalid syntax >>> for i in range(5): ... print(i) ... 0 1 2 3 4 >>> print(0) 0
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
that doesn't work, either:

Output:
Python 3.6.8 (default, Jan 14 2019, 11:02:34) [GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux Type "help", "copyright", "credits" or "license" for more information. >>> a=[1,2,3,4,9,6] >>> b=[0,5,10] >>> for x in b: ... if x in a: ... print('foo') ... break ... >>> else: File "<stdin>", line 1 else: ^ SyntaxError: invalid syntax >>>
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
The end of a for/else is after the else block:
>>> a=[1,2,3,4,5,6]
>>> b=[0,5,10]
>>> for x in b:
...     if x in a:
...         print('foo')
...         break
... else:
...     print('bar')
...
foo
>>> print('xyzzy')
xyzzy
Reply
#5
why does the interactive interpreter use a different syntax than the script interpreter?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interactive plots that can be embed in web page mouse9095 1 571 Jun-12-2023, 04:51 PM
Last Post: deanhystad
  things that work in terminal mode but not in sublime mode alok 4 2,816 Aug-11-2021, 07:02 PM
Last Post: snippsat
  interactive map and function hurc60248 1 1,722 Jan-08-2021, 09:22 PM
Last Post: Larz60+
  Interactive Menu, String Search? maggotspawn 3 2,541 May-11-2020, 05:25 PM
Last Post: menator01
  Problem with interactive python prompt Groot_04 4 3,090 Aug-02-2018, 08:39 AM
Last Post: Groot_04
  tale interactive fiction - looking for tutorial aryell8linx 1 2,608 May-29-2018, 12:41 PM
Last Post: Larz60+
  Filtering an interactive CLI command via pipe(s) Skaperen 2 4,115 Nov-23-2016, 09:17 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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