好的,所以我正在尝试编写一个程序,当两辆车以特定速度相互加速时,可以找到汽车的时间,每辆车的速度不同,只有使用时间=距离/速率给定速率。但是我需要一个带有 Unresolved 变量的表达式来解决如下问题:
Car1 的时速为 20mph。 Car2 比 Car1 快 10mph。 Car2 比 Car1 晚一个小时离开。 Car2 需要多长时间才能 catch Car1?
我的工作没有程序:
t = Travel time total (in hours)
10*t = 20(t-1) # Note the 1 is for the 1 hour
# rearrange
10*t = 20*t - 20
# subtract 10*t from both sizes
0 = 10*t - 20
# add 20 to both sides
20 = 10*t
#divide both sizes by 10
2 = t
是否有一个模块支持使用分配属性对 undefined variable 进行操作(即可以求解像这样的方程式)?
如果没有,我可以举一个小例子说明如何在没有模块的情况下使用未识别的变量吗?我刚在学校复习过这个。
请您参考如下方法:
您要查找的模块称为 SymPy .
import sympy
t = sympy.Symbol('t') # set t as a variable/symbol
sympy.solve(sympy.Eq(10*t, 20*(t-1)), t) # which reads: solve the equation 10*t == 20*(t-1) for t.
# returns [2] (list of solutions to the equation)
(查看更多关于使用 SymPy 的 quick examples)。
