我已经编写了这个程序,所以根据用户定义的值求解两个方程。常量 kx 和 ky,我定义为浮点数。对于范围 - 变量开始和结束 - 我希望用户输入一个数字,或者像 6 * np.pi (6Pi) 这样的东西。就像现在一样,我收到以下错误。如何定义此变量以允许用户输入多种类型的输入?谢谢!
Traceback (most recent call last):
File "lab1_2.py", line 11, in <module>
x = np.linspace(start, end, 256, endpoint=True)
File "/Library/Frameworks/Python.framework/Versions/7.3/lib/python2.7/site- packages/numpy/core/function_base.py", line 80, in linspace
step = (stop-start)/float((num-1))
TypeError: unsupported operand type(s) for -: 'str' and 'float'
这是代码:
from pylab import *
import numpy as np
kx = float(raw_input("kx: "))
ky = float(raw_input("ky: "))
print "Define the range of the output:"
start = float(raw_input("From: "))
end = float((raw_input("To: "))
x = np.linspace(start, end, 256, endpoint=True)
y = np.linspace(start, end, 256, endpoint=True)
dz_dx = ((1 / 2.0) * kx * np.exp((-kx * x) / 2)) * ((2 * np.cos(kx *x)) - (np.sin(kx * x)))
dz_dy = ((1 / 2.0) * ky * np.exp((-ky * y) / 2)) * ((2 * np.cos(ky *y)) - (np.sin(ky * y)))
plot(x, dz_dx, linewidth = 1.0)
plot(y, dz_dy, linewidth = 1.0)
grid(True)
show()