python - IDLE 注册变量失败。这怎么可能?

标签 python python-idle

我在 Microsoft Windows 7 上使用 python 2.7.6 和 IDLE 2.7.6。

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. 我输入 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 2.7 的 IDLE 版本中时,它会忽略第一个语句之后的所有语句。因此,您的 x5 行永远不会运行,并且该名称未绑定(bind)任何内容。

您可以看到动画中 >>> 字符的颜色在 x5 行的开头与其他行不同。当它们被复制时,它们像其他输入的文本一样是黑色的。当它们是实际提示时,它们是红色的(至少在默认配色方案中是这样)。

您可以通过仅突出显示一行代码并按 ENTER 仅复制它而不是它所属的整个条目来避免此问题。

在 Python 3 中,在 IDLE 控制台中同时提供多个语句是错误的(它说 SyntaxError: multiple statements found while compiling a single statement)。

关于python - IDLE 注册变量失败。这怎么可能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40493106/

相关文章:

python - 直接从命令行在 IDLE 窗口中运行文件

python - 为 Django 存档页面选择不同的年份和月份

python - 没有使用 gevent 加速读取文件

python - NumPy:以 n 为底的对数

python - 从 python 脚本启动 python IDLE

python - 程序包只能在IDLE或解释器中正常工作,而不能独立运行吗?

python - ModuleNotFoundError : No module named 'pgzrun' when I have it installed?

Python turtle 程序在 IDLE 下工作,否则不工作

python - 如何处理 sklearn GradientBoostingClassifier 中的分类变量?

python - 如何将相关子列表的唯一ID分配给共享评论元素的子列表的每个元素?