Search code examples
pythonpython-idle

IDLE failed to register a variable. How is that possible?


I use python 2.7.6 with IDLE 2.7.6 on Microsoft Windows 7.

IDLE failed to register a variable (named x5), as shown in the image below. How is that possible?

enter image description here


Actions taken to trigger the issue:

  1. The following is already in my IDLE console history:

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

  2. I place the cursor somewhere on x5 = 4

  3. I hit ENTER
  4. This copies the following:

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

  5. After copying, the cursor is automatically placed at the end of x5 = 4. I hit ENTER again, hoping that x5 takes the value 4.

  6. I type x5 to check the value of x5. I get

    >>> x5

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


Based on Blckknght's answer and comments, it looks like I had copy pasted the following two lines at once (including the >>>) in the IDLE console in the first place:

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

As a result, the line >>> x5 = 4 was simply ignored, since IDLE only cares about the first python statement it sees.


The entire IDLE log:

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
>>> 

Solution

  • You're copying multiple lines at once. When that happens in Python 2.7's version of IDLE, it ignores all the statements after the first. So your x5 line never runs, and the name is not bound to anything.

    You can see that the color of the >>> characters in your animation at the start of the x5 lines are different than one the other lines. When they're copied they are black like other entered text. When they are an actual prompt, they're red (in the default color scheme at least).

    You can avoid this issue by highlighting just one line of code and pressing ENTER to copy only it, not the whole entry it was part of.

    In Python 3, it's an error to provide multiple statements at the same time in the IDLE console (it says SyntaxError: multiple statements found while compiling a single statement).