Tom*_*Tom 1 python math variables algebra
好了,我试图让一个程序,可以找到时间车的时候两台车以一定的速度加速远离彼此,每个与仅给出了不同的速率率的使用时间=距离/速度.但我需要一个带有未解决变量的表达式来执行以下问题:
Car1的速度为20英里/小时.Car2的速度比Car1快10mph.Car2比Car1晚了一个小时.Car2赶上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
Run Code Online (Sandbox Code Playgroud)
是否有一个模块使用分配属性支持未定义变量的操作(即哪个可以解决像这样的方程式)?
如果没有,我可以举一个小例子来说明如何在没有模块的情况下使用未识别的变量吗?我刚刚在学校接过这个.
您正在寻找的模块称为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)
Run Code Online (Sandbox Code Playgroud)
(请参阅使用SymPy的更多快速示例).