 Alex Brown - 3 years ago 80
Python Question

# Solving a physics equation in Python

I'm fairly new to Python, and I got an idea to write a program to solve the Kinematic Equations used in physics.

``````vi = input("What is the initial velocity?")

if vi == "/":
dontuse = "vi"
else:
pass
``````

I used this code for each of the values needed (Displacement, Initial Velocity, Final Velocity, Acceleration, and Time)

If the user inputs / as the value, it will not be used in the equation, so I wrote a small assignor to decide which equation to use.

``````if dontuse == "a":
eq3()
elif dontuse == "d":
eq4()
elif dontuse == "vf":
eq1()
elif dontuse == "t":
eq2()
``````

Initial Velocity (vi) is used in every equation, so I didnt need to add one for that.

``````def eq1():
# d = Vi*t + 1/2*a*t^2
print("Equation 1!")
answer = # d = Vi*t + 1/2*a*t^2
``````

My question is here, how would I insert the values of the other variables into an equation that the comupter could solve, then print out?

It may seem like a basic question, but I wasnt sure how to do algebra like this with with Python. intboolstring
``````def eq1():
# d = Vi*t + 1/2*a*t^2
print("Equation 1!")
answer = # d = Vi*t + 1/2*a*t^2
``````

To solve the equation `d = Vi*t + 1/2*a*t^2`in python, you would do

``````answer = Vi*t + .5*a*(t**2)
``````

How does this work??

1. Multiplies initial velocity by time
2. Multiplies 1/2 by a
3. Multiplies that quantity (step 2) by the square of t

For the other equations, you really want to solve for one variable, so:

``````t = (Vf-Vi)/a
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download