The code in each one of the questions below has an error in it. What error message is produced by each code snippet? Assume the numpy and matplotlib.pyplot modules have been imported as:
import numpy as np import matplotlib.pyplot as plt
y = np.sin('np.pi')
x = np.array([1.2,3.4,6.1]) print(x[4])
x = np.array([1.2,3.4,6.1) print(x[2])
x = 1 y = 0 z = x/y
from numpy import np
x = np.linspace(0,10*np.pi,1000) y = np.snc(x)
Suppose you run the program junk.py from Spyder by clicking the green arrow and you get the following error message:
runfile('/Users/sburns/junk.py', wdir='/Users/sburns') Traceback (most recent call last): File "<ipython-input-53-cb18af20b767>", line 1, in <module> runfile('/Users/sburns/junk.py', wdir='/Users/sburns') File "/Users/sburns/anaconda3/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 668, in runfile execfile(filename, namespace) File "/Users/sburns/anaconda3/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "/Users/sburns/junk.py", line 17, in <module> plotit(x,y) File "/Users/sburns/junk.py", line 13, in plotit plt.plot(x,y) File "/Users/sburns/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py", line 3363, in plot ret = ax.plot(*args, **kwargs) File "/Users/sburns/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py", line 1867, in inner return func(ax, *args, **kwargs) File "/Users/sburns/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 1528, in plot for line in self._get_lines(*args, **kwargs): File "/Users/sburns/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 406, in _grab_next_args for seg in self._plot_args(this, kwargs): File "/Users/sburns/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 383, in _plot_args x, y = self._xy_from_xy(x, y) File "/Users/sburns/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 242, in _xy_from_xy "have shapes {} and {}".format(x.shape, y.shape)) ValueError: x and y must have same first dimension, but have shapes (1000,) and (1,)
On what line number in your script are you likely to find your error?
Will the following code generate an error? (If you're not sure, try it.)
import numpy as np a = [1,2,3] b = np.array([4,5,6]) c = [4,5,6] d = a + b e = a + c x = d - e print(x)
What will happen if you try to run the code below? (Try it if you're not sure.)
def isInfinity(x,y): try: x/y except ZeroDivisionError: return True return False x = 1 y = 0 myValue = isInfinity(x,y)
What will happen if you try to run the code below? (Try it if you're not sure.)
def isInfinity(x,y): try: x/y except ZeroDivisionError: return True return False x = 1 y = 1 myValue = isInfinity(x,y)