Python Anywhere 的 Python 语法错误 [英] Python Syntax Errors with Python Anywhere

查看:32
本文介绍了Python Anywhere 的 Python 语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Python 与 DataCamp 和 Python Anywhere 一起使用,他们似乎对什么是语法错误存在分歧.我最近刚刚开始,所以我尝试了这行代码:

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!')

它在 DataCamp 上的运行就像它应该的那样,但是使用 Python Anywhere,我收到以下错误:

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.

推荐答案

粘贴到交互式解释器中时,在块语句之后必须有一个空行,在下一个声明.这是嵌入在 http://www.python.org 上的 Python Anywhere 解释器的输出:

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
>>> 

将第一列中的任何内容写入 ... 将导致此 SyntaxError,即使在源文件中是合法的.这是因为所有复合语句都传递到 exec(compile(... 'single')) 完成时;并且python REPL在这里有点愚蠢,认为它只是一个语句,而实际上它是while后跟一个print.

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.

print 将修复交互式解释器中的问题之前,按 Enter 键使提示返回到 >>>:

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!
>>> 

但是请注意,while 循环现在会在复合语句终止后立即运行,即在 >>>> 提示再次显示之前.

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

除了标准的 Python REPL 之外,还有其他 shell.一种流行的 ipython 有一个控制台外壳,可以识别复制粘贴的内容并正确运行:

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]: 

这篇关于Python Anywhere 的 Python 语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆