In the exercise https://www.codecademy.com/courses/learn-python-3/lessons/modules-python/exercises/modules-python-namespaces on Task 4 I first created the variable with a for loop successfully. The code ran without an error. I then decided to practice creating the variable with a list comprehension, and that too ran as expected and no error was thrown. I then wanted to practice creating the variable with a while loop and that also was successful, but after the list of 12 sample numbers was randomly created, my next line of code (plt.plot(numbers_a, numbers_b)
) throws the following error.
Traceback (most recent call last): File "script.py", line 17, in <module> plt.plot(numbers_a, numbers_b) File "/usr/local/lib/python3.6/dist-packages/matplotlib/pyplot.py", line 3317, in plot ret = ax.plot(*args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/matplotlib/__init__.py", line 1898, in inner return func(ax, *args, **kwargs) File "/usr/local/lib/python3.6/dist-packages/matplotlib/axes/_axes.py", line 1406, in plot for line in self._get_lines(*args, **kwargs): File "/usr/local/lib/python3.6/dist-packages/matplotlib/axes/_base.py", line 407, in _grab_next_args for seg in self._plot_args(remaining, kwargs): File "/usr/local/lib/python3.6/dist-packages/matplotlib/axes/_base.py", line 385, in _plot_args x, y = self._xy_from_xy(x, y) File "/usr/local/lib/python3.6/dist-packages/matplotlib/axes/_base.py", line 244, in _xy_from_xy "have shapes {} and {}".format(x.shape, y.shape)) ValueError: x and y must have same first dimension, but have shapes (12,) and (13,)
.
Why does plt.plot(numbers_a, numbers_b)
throw this error for a list created from a while loop, but not for a list created by a for loop or list comprehension?