Luc Luc - 6 months ago 18
Python Question

Why do I get syntax error in the following code

This gives an syntax error:

import numpy as np
import scipy.interpolate
import matplotlib.pyplot as plt
y = np.array([-­0.23122875, -­0.11375591, -­0.09760733, -­0.07401004, ­-0.03565704,-­0.02449268,­-0.01411058,0.0018971,-­0.0145346,0.00156783,0.0048691,0.01990767,0.02048657,0.01691803,0.02228818,0.02578349,0.02707902,0.01996198])
x = np.array(range(len(y)))
stepsize = 0.0001
xvals = np.arange(0,13.0001,stepsize)
func = scipy.interpolate.interp1d(x, y, kind = "cubic")
yvals = func(xvals)
plt.plot(xvals,yvals)
plt.plot(x,y, "o")
dif = np.diff(yvals)
for i in range(len(y))
if i == 0:
print dif[int(i*1/stepsize)]
else:
print dif[int(i*1/stepsize)-1]


It gives an syntax error in line 5 like this


y = np.array([-­0.23122875, -­0.11375591, -­0.09760733, -­0.07401004, ­-0.03565704,-­0.02449268,­-0.01411058,0.0018971,-­0.0145346,0.00156783,0.0048691,0.01990767,0.02048657,0.01691803,0.02228818,0.02578349,0.02707902,0.01996198])
^
SyntaxError: invalid syntax


I just do not know why. When I change some of the values in y, it suddenly does work.

Thanks in advance

Answer

You appear to be using a soft hyphen on this line:

y = np.array([-­0.23122875, -­0.11375591, -­0.09760733, -­0.07401004, ­-0.03565704,-­0.02449268,­-0.01411058,0.0018971,-­0.0145346,0.00156783,0.0048691,0.01990767,0.02048657,0.01691803,0.02228818,0.02578349,0.02707902,0.01996198])

You need to switch it to a hyphen-minus like this:

y = np.array([-0.23122875, -0.11375591, -0.09760733, -0.07401004, -0.03565704,-0.02449268,-0.01411058,0.0018971,-0.0145346,0.00156783,0.0048691,0.01990767,0.02048657,0.01691803,0.02228818,0.02578349,0.02707902,0.01996198])
Comments