2016-11-08 44 views
3

我在Microsoft Windows 7上使用python 2.7.6和IDLE 2.7.6。IDLE未能註冊一個變量。這怎麼可能?

IDLE未能註冊一個變量(名爲x5),如下圖所示。這怎麼可能?

enter image description here


觸發問題所採取

操作:

  1. 以下是已經在我的IDLE控制檯歷史記錄:

    >>> x1 = np.arange(9.0).reshape((3, 3))
    >>> x5 = 4

  2. 我把光標放在某處x5 = 4

  3. 我打ENTER
  4. 此份以下:

    >>> x1 = np.arange(9.0).reshape((3, 3))
    >>> x5 = 4

  5. 複製後,光標會自動放置在x5 = 4結束。我打一次ENTER,希望x5取值4.

  6. I型x5檢查的x5值。我得到

    >>> x5

    Traceback (most recent call last):
    File "<pyshell#118>", line 1, in <module>
    x5
    NameError: name 'x5' is not defined


基於Blckknght的回答和評論,它看起來就像我複製擺在首位的IDLE控制檯一次(包括>>>)粘貼以下兩行:

x1 = np.arange(9.0).reshape((3, 3)) 
>>> x5 = 4 

其結果是,該行>>> x5 = 4被簡單地忽略,因爲IDLE只關心它看到的第一條python聲明。


整個IDLE日誌:

Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win32 
Type "copyright", "credits" or "license()" for more information. 
>>> '#' *3 
'###' 
>>> import numpy as np 
>>> a=np.random.random((3, 3)) 
>>> b=np.random.random((3, 3)) 
>>> a 
array([[ 0.92731349, 0.48377097, 0.32175387], 
     [ 0.65061308, 0.72905101, 0.65499733], 
     [ 0.1334612 , 0.4023123 , 0.50179231]]) 
>>> b 
array([[ 0.35232561, 0.77837691, 0.70645223], 
     [ 0.51114669, 0.83303289, 0.78842767], 
     [ 0.51179183, 0.25109079, 0.91229917]]) 
>>> a*b 
array([[ 0.32671629, 0.37655615, 0.22730374], 
     [ 0.33255872, 0.60732347, 0.51641802], 
     [ 0.06830435, 0.10101691, 0.45778471]]) 
>>> a = [[1, 0], [0, 1]] 

>>> b = [[4, 1], [2, 2]] 

>>> a = [[3, 4], [2, 1]] 
>>> a 
[[3, 4], [2, 1]] 
>>> b 
[[4, 1], [2, 2]] 
>>> np.dot(a, b) 

    File "<pyshell#12>", line 1 
    np.dot(a, b) 
^
IndentationError: unexpected indent 
>>> np.dot(a, b) 
array([[20, 11], 
     [10, 4]]) 
>>> a = [[1, 2]] 
>>> a.dot(b) 

Traceback (most recent call last): 
    File "<pyshell#15>", line 1, in <module> 
    a.dot(b) 
AttributeError: 'list' object has no attribute 'dot' 
>>> a = np.array([[1, 2]]) 
>>> a.dot(b) 
array([[8, 5]]) 
>>> a = np.array([[3,4]]) 
>>> b= = np.array([[3,4]]) 
SyntaxError: invalid syntax 
>>> b= np.dot(a, b) 
np.array([[3,4]]) 

>>> a*b 
array([[60, 44]]) 
>>> a 
array([[3, 4]]) 
>>> b 
array([[20, 11]]) 
>>> np.dot(a, b) 

Traceback (most recent call last): 
    File "<pyshell#24>", line 1, in <module> 
    np.dot(a, b) 
ValueError: objects are not aligned 
>>> 
>>> x1 = np.arange(9.0).reshape((3, 3)) 
>>> x2 = np.arange(3.0) 

>>> np.multiply(x1, x2) 

array([[ 0., 1., 4.], 
     [ 0., 4., 10.], 
     [ 0., 7., 16.]]) 
>>> x1 
array([[ 0., 1., 2.], 
     [ 3., 4., 5.], 
     [ 6., 7., 8.]]) 
>>> z2 

Traceback (most recent call last): 
    File "<pyshell#30>", line 1, in <module> 
    z2 
NameError: name 'z2' is not defined 
>>> x2 
array([ 0., 1., 2.]) 
>>> x1 = np.arange(9.0).reshape((3, 3)) 
>>> x2 = np.arange(9.0).reshape((3, 3)) 
>>> np.multiply(x1, x2) 
array([[ 0., 1., 4.], 
     [ 0., 4., 10.], 
     [ 0., 7., 16.]]) 
>>> x1 
array([[ 0., 1., 2.], 
     [ 3., 4., 5.], 
     [ 6., 7., 8.]]) 
>>> x2 
array([ 0., 1., 2.]) 
>>> x2 
array([ 0., 1., 2.]) 
>>> x1 = np.arange(9.0).reshape((3, 3)) 
>>> x2 = np.arange(9.0).reshape((3, 3)) 
>>> x2 
array([ 0., 1., 2.]) 
>>> x1 
array([[ 0., 1., 2.], 
     [ 3., 4., 5.], 
     [ 6., 7., 8.]]) 
>>> x2 
array([ 0., 1., 2.]) 
>>> x1 = np.arange(9.0).reshape((3, 3)) 
>>> x2 = np.arange(9.0).reshape((3, 3)) 
>>> x2 
array([ 0., 1., 2.]) 
>>> x1 = np.arange(9.0).reshape((3, 3)) 
>>> x2 = np.arange(9.0) 
>>> x2 
array([ 0., 1., 2.]) 
>>> x1 = np.arange(9.0).reshape((3, 3)) 
>>> x2 = 3 
>>> x2 
array([ 0., 1., 2.]) 
>>> x1=1 
>>> x1 
1 
>>> x2 = 3 
>>> x2 
3 
>>> x1 = np.arange(9.0).reshape((3, 3)) 
>>> x2 = np.arange(9.0) 
>>> x2 
3 
>>> x1 = np.arange(9.0).reshape((3, 3)) 
>>> x2 = np.arange(9.0) 
>>> x2 
3 
>>> x1 
array([[ 0., 1., 2.], 
     [ 3., 4., 5.], 
     [ 6., 7., 8.]]) 
>>> x1x1 = np.arange(9.0).reshape((3, 3)) 
>>> x2 = np.arange(9.0) 
>>> x1 = np.arange(9.0).reshape((3, 3)) 
>>> x2 = np.arange(9.0) 
>>> x1 = np.arange(9.0).reshape((3, 3)) 

>>> x1 = np.arange(9.0).reshape((3, 3)) 
>>> x3 = np.arange(9.0) 
>>> x3 

Traceback (most recent call last): 
    File "<pyshell#60>", line 1, in <module> 
    x3 
NameError: name 'x3' is not defined 
>>> x1 = np.arange(9.0).reshape((3, 3)) 
>>> x3 = 4 
>>> x3 

Traceback (most recent call last): 
    File "<pyshell#62>", line 1, in <module> 
    x3 
NameError: name 'x3' is not defined 
>>> x1 = np.arange(9.0).reshape((3, 3)) 
>>> x4 = 4 
>>> x4 

Traceback (most recent call last): 
    File "<pyshell#64>", line 1, in <module> 
    x4 
NameError: name 'x4' is not defined 
>>> 
>>> 


>>> 



>>> 

>>> 


>>> 

>>> 

>>> 

>>> 

>>> 

>>> 

>>> 

>>> x1 = np.arange(9.0).reshape((3, 3)) 
>>> x4 = 4 
>>> x4 

Traceback (most recent call last): 
    File "<pyshell#78>", line 1, in <module> 
    x4 
NameError: name 'x4' is not defined 
>>> x4 = 4 
>>> x4 
4 
>>> x1 = np.arange(9.0).reshape((3, 3)) 
>>> x4 = 4 
>>> x1 = np.arange(9.0).reshape((3, 3)) 
>>> x5 = 5 
>>> x1 = np.arange(9.0).reshape((3, 3)) 
>>> x5 = 4 
>>> x5 

Traceback (most recent call last): 
    File "<pyshell#84>", line 1, in <module> 
    x5 
NameError: name 'x5' is not defined 
>>> x1 = np.arange(9.0).reshape((3, 3)) 
>>> x5 = 4 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> 
>>> x1 = np.arange(9.0).reshape((3, 3)) 
>>> x5 = 4 
>>> x1 = np.arange(9.0).reshape((3, 3)) 
>>> x5 = 4 
>>> x5 

Traceback (most recent call last): 
    File "<pyshell#104>", line 1, in <module> 
    x5 
NameError: name 'x5' is not defined 
>>> 
+0

這是python,而不是IDLE執行您的代碼,並且這是生成錯誤消息。你的安裝似乎有問題。如果你在沒有IDLE的交互式Python中做同樣的事情會發生什麼?請將交互複製並粘貼到您的問題中。看起來很刺激的視頻是不可能的。我強烈建議你升級到2.7.12。 –

+0

@TerryJanReedy只有當我使用ENTER鍵盤快捷鍵複製以前的代碼時,纔會出現此問題。如果我輸入'x5 = 4',那麼它確實有效。所以我認爲IDLE正在搞亂一些東西。我已經在問題中完成了整個IDLE日誌。 –

+0

您的一個'x5'名稱旁邊有一個隱形控制字符嗎?如果您複製並粘貼名稱而不是其他名稱,則可以解釋該問題。 – Blckknght

回答

5

你一次複製多條線路。當Python 2.7版本的IDLE發生這種情況時,它會忽略第一個之後的所有語句。所以你的x5行永遠不會運行,並且該名稱不受任何約束。

您可以看到x5行開始處動畫中的>>>字符的顏色與其他行不同。當他們被複制時,他們像其他輸入的文本一樣黑。當它們是實際提示時,它們是紅色的(至少在默認顏色方案中)。

您可以通過突出顯示一行代碼並按ENTER來僅複製它,而不是它所屬的整個條目來避免此問題。

在Python 3中,在IDLE控制檯(它說SyntaxError: multiple statements found while compiling a single statement)中同時提供多個語句是錯誤的。

+0

謝謝,這是有道理的(儘管我不明白爲什麼IDLE會選擇只執行第一行)。順便說一句,如何點擊一行並按下一次複製多行? –

+1

據我所知,只有在先前版本的行同時粘貼的情況下(可能使用其他地方的CTRL + V),纔會使用ENTER-to-Copy方法。 – Blckknght

+0

謝謝,一切都很清楚,我現在可以完全重現我在問題中顯示的行爲。 –

相關問題