On 04/02/17 22:56, boB Stepp wrote: > On Sat, Feb 4, 2017 at 4:40 PM, David <bouncingc...@gmail.com> wrote: >> On 5 February 2017 at 09:02, boB Stepp <robertvst...@gmail.com> wrote: >>> py3: a >>> ['Mary', 'had', 'a', 'little', 'lamb', 'break'] >>> py3: for w in a: >>> ... print(w) >>> ... print('Huh?') >>> File "<stdin>", line 3 >>> print('Huh?') >>> ^ >>> SyntaxError: invalid syntax >>> >>> I don't understand why this throws a SyntaxError. If I wrap >>> essentially the same code into a function it works:
Because you never completed the for loop. The interactive interpreter tries to exercise each Python statement as it goes but it cannot exercise the loop until it sees the end of the block. Your second print statement is outside the block but the block has not yet been executed so it sees an error. If you put a blank line it will work OK. Alternatively if you put your code inside a function definition it will understand it because it interprets the full definition. Then when you call the function it executes it as a whole. There is no discrepency in what the interpreter is trying to interpret. But when you do it at the top level the interpreter is still waiting for the end of the loop. Does that help? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor