0

I use the IronPython Console when I feel like programming, but it does some wacky stuff. For example:

If a=("X")

it says "Unexpected token '=.' Or this:

If a is ("X"):
    print ("Y")
else:
    print ("Z")

But it should end after that, it still puts in "...". Why?

4

2 に答える 2

9

最初の質問:

if a=("X"):

有効なPythonコードではありません。あなたはおそらく意味しました:

if a == ("X"):

2つ目は、REPL(read-eval-print loop-the shell)は、空の行が表示されるまで、ブロックをいつ終了するかを認識しません。例えば:

>>> if a == "X":
...     print "Y"
... else:
...     print "Z"
... 

次の行に別のステートメントを入力することもできます。空白のままにすると、REPLは、そのブロックが完了し、新しいブロックを開始したいことを認識します。これは、Pythonの重要な空白の副作用です。

于 2012-06-11T18:02:20.380 に答える
1

そのはず:

if x==('x'):
    print('x')

これは、=が割り当てであるためです。==比較です。

于 2012-06-11T20:26:45.760 に答える