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


Messages In This Thread
for / else not working in interactive mode - by Skaperen - Jul-17-2019, 12:24 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  things that work in terminal mode but not in sublime mode alok 4 3,132 Aug-11-2021, 07:02 PM
Last Post: snippsat
  interactive map and function hurc60248 1 1,863 Jan-08-2021, 09:22 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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