我在Microsoft Windows 7上使用python 2.7.6和IDLE 2.7.6。IDLE未能註冊一個變量。這怎麼可能?
IDLE未能註冊一個變量(名爲x5
),如下圖所示。這怎麼可能?
觸發問題所採取
操作:
以下是已經在我的IDLE控制檯歷史記錄:
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x5 = 4
我把光標放在某處
x5 = 4
- 我打ENTER
此份以下:
>>> x1 = np.arange(9.0).reshape((3, 3))
>>> x5 = 4
複製後,光標會自動放置在
x5 = 4
結束。我打一次ENTER,希望x5
取值4.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
>>>
這是python,而不是IDLE執行您的代碼,並且這是生成錯誤消息。你的安裝似乎有問題。如果你在沒有IDLE的交互式Python中做同樣的事情會發生什麼?請將交互複製並粘貼到您的問題中。看起來很刺激的視頻是不可能的。我強烈建議你升級到2.7.12。 –
@TerryJanReedy只有當我使用ENTER鍵盤快捷鍵複製以前的代碼時,纔會出現此問題。如果我輸入'x5 = 4',那麼它確實有效。所以我認爲IDLE正在搞亂一些東西。我已經在問題中完成了整個IDLE日誌。 –
您的一個'x5'名稱旁邊有一個隱形控制字符嗎?如果您複製並粘貼名稱而不是其他名稱,則可以解釋該問題。 – Blckknght