我正在编写一个简单的python脚本来使用Eulers方法求解微分方程,现在每当我想求解一个新的方程时,都必须更改源代码。
(由于这是一个私人项目,所以目前安全问题并不重要。)
我的问题:
是否可以输入一个方程式作为用户输入并使其成为变量?例如,在输入y/x的位置使用e=input("enter equation"),并将e设为变量供以后使用?#I would like to be able to type in a equation as user input and turn it to a variable for later use in the if and elif segments, example
e=input("Enter your equation here")
e=float(e)
h=input("Ange steglängd: ")
h=float(h)
x=input("Angivet värde på x: ")
x=float(x)
y=input("Värde på y med anseende på x: ")
y=float(y)
z=input("Närmevärde du vill ha för y(x), ange x: ")
z=float(z)
if x
while x
#Type in equation below as e
e=y/x
y=y+h*e
x+=h
print(y)
elif x>z:
while x>z:
e=y/x
y=y-h*e
x-=h
print(y)