0

I'm using Python with DataCamp and Python Anywhere, and they seem to disagree on what a syntax error is. I've recently just started, so I tried this line of code:

n = 5
while n > 0:
   print (n)
   n = n - 1
print ('Blastoff!')

It runs like it's supposed on DataCamp, but with Python Anywhere, I get the following error:

  File "<stdin>", line 5
    print ("Blastoff!")
        ^
SyntaxError: invalid syntax

I don't know what it's referencing or trying to tell me. The error message is unhelpful, and I don't know why I'm getting two different evaluations here.

vaultah
  • 44,105
  • 12
  • 114
  • 143
  • Are you running python2.x ? – Arpit Solanki Sep 02 '17 at 17:23
  • 4
    @ArpitSolanki: this would work in Python 2. – Martijn Pieters Sep 02 '17 at 17:23
  • Are you running this in an interactive interpreter? Then you must include a blank line between the `n = n - 1` and the `print` line. – Martijn Pieters Sep 02 '17 at 17:24
  • It's possible that the Python Anywhere service verifies that your code is valid by its standards before the interpreter executes it. If that's the case, it could be that they don't want the space after your `print`s: `print('Blastoff!')`, instead. Although, I'm not sure why it wouldn't raise the same error for `print (n)` (unless it does and this one is just masking it). – Zach Gates Sep 02 '17 at 17:25
  • @ZachGates: The spaces are not a problem. – Martijn Pieters Sep 02 '17 at 17:26
  • 1
    The code you're running is not the code that you posted - the quotes are different. I would, therefore, suspect that the error is actually earlier in your code (perhaps an unclosed quote or bracket) – Glenn Sep 03 '17 at 12:31
  • The line `File "", line 5` is a hint that the code is run in an interactive shell. – Nils Werner Dec 17 '17 at 07:40

2 Answers2

2

When pasting into interactive interpreter, you must have an empty line after a block statement, before the next statement. Here's the output from the Python Anywhere interpreter embedded on http://www.python.org:

Python 3.6.0 (default, Jan 13 2017, 00:00:00) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> n = 5
>>> while n > 0:
...    print (n)
...    n = n - 1
... print ('Blastoff!')
  File "<stdin>", line 4
    print ('Blastoff!')
        ^
SyntaxError: invalid syntax
>>> 

Writing anything in the first column to ... will cause this SyntaxError, even though legal in a source file. This is because all compound statements are passed into exec(compile(... 'single')) when completed; and the python REPL is being a bit stupid here, thinking that it was just one statement, when it is in fact while followed by a print.

Hitting enter so that the prompt returns to >>> before print will fix the problem in interactive interpreters:

Python 3.6.0 (default, Jan 13 2017, 00:00:00) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> n = 5
>>> while n > 0:
...    print (n)
...    n = n - 1
... 
5
4
3
2
1
>>> print ('Blastoff!')
Blastoff!
>>> 

But notice that the while loop now runs as soon as the compound statement is terminated, i.e. before the >>> prompt shows again.

There are other shells besides the standard Python REPL. One popular, ipython, has a console shell that will recognize copy-pasted content and run this one correctly:

% ipython
Python 3.5.3 (default, Jan 19 2017, 14:11:04) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.1.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: n = 5
   ...: while n > 0:
   ...:    print (n)
   ...:    n = n - 1
   ...: print ('Blastoff!')
   ...: 
5
4
3
2
1
Blastoff!

In [2]: 
0

Both PythonAnywhere and the shell will treat anything within the ... as part of the first statement, meaning that anything after a if or with or while or for that starts with 3 dots is supposed to be executed when the first statement is evaluated.

If you had an if statement, any code entered while the ... is there will be executed when the if statement is evaluated.

Pokestar Fan
  • 174
  • 1
  • 12