とき対話インタプリタにを貼り付けるには、次のステートメントの前に、ブロック文の後空行を持っている必要があります。ここではどこでも通訳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'))
に渡されるためです。実際にはwhile
の後にprint
が続きますが、Python REPLはここで少しばかげています。
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
ループは今とすぐ複合文が終了したとして実行されることに気づく:
打つにはprint
前>>>
にプロンプトに戻りますが、インタラクティブな通訳で問題を修正するように入力します。 >>>
プロンプトが再び表示されます。
標準のPython REPL以外のシェルもあります。何ということを意味し、
% 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]:
python2.xを実行していますか? –
@ArpitSolanki:これはPython 2で動作します。 –
対話型インタプリタでこれを実行していますか?次に、 'n = n - 1'と' print'行の間に空白行を入れる必要があります。 –