import numpy as np
import matplotlib.pyplot as plt
print("FUNCTION GRAPHER")
def graph(formula,domain):
x = np.array(domain)
y = eval(formula)
plt.plot(x, y)
plt.show()
formula=input("Function: ")
domainmin=float(input("Min X Value: "))
domainmax=float(input("Max X Value: "))
graph(formula, range(domainmin,domainmax))
这是我创建grapher的代码。如您所见,用户可以输入函数和域。我正在使用一个简单的 y=x 函数对其进行测试,这就是我得到的错误。我猜这与我如何设置输入有关。
FUNCTION GRAPHER
Function: x
Min X Value: -1
Max X Value: 10
Traceback (most recent call last):
File "/Users/William/Documents/Science/PYTHON/Grapher.py", line 12, in <module>
graph(formula, range(domainmin,domainmax))
TypeError: 'float' object cannot be interpreted as an integer
发生错误后,我输入以下内容:
graph('x', range(-1,10))
并且图表弹出。我正在使用 Python 3.4。