Skip to content

Instantly share code, notes, and snippets.

@JellyWX
Last active May 21, 2020 21:20
Show Gist options
  • Save JellyWX/60f31ce76e3fa410b311f5bd5ab4a34d to your computer and use it in GitHub Desktop.
Save JellyWX/60f31ce76e3fa410b311f5bd5ab4a34d to your computer and use it in GitHub Desktop.
a python script to calculate acceleration from time or distance when provided with any 1 missing value
from math import sqrt
## V**2 - U**2 = 2*A*X ##
## A = (v-u)/T ##
def main():
dis = str(input('Are you working for distance or time? > '))
v = input('Enter final velocity (m/s) > ')
u = input('Enter initial velocity (m/s) > ')
a = input('Enter acceleration (ms**-1) > ')
x = input('Enter distance or time (m or s) > ')
if dis in 'distance':
vuax(v,u,a,x)
elif dis in 'time':
vuat(v,u,a,x)
def vuax(v,u,a,x):
if error_test(a,float) == False:
print('acceleration value is ' + str( ( float(v)**2 - float(u)**2 ) / ( 2 * float(x) ) ) + 'm/s**2')
elif error_test(v,float) == False:
print('final velocity value is ' + str( sqrt( ( 2 * float(a) * float(x) ) + float(u)**2 ) ) + 'm/s')
elif error_test(u,float) == False:
print('initial velocity value is ' + str( sqrt( float(v)**2 - ( 2 * float(a) * float(x) ) ) ) + 'm/s')
elif error_test(x,float) == False:
print('distance value is ' + str( ( float(v)**2 - float(u)**2 ) / ( 2 * float(a) ) ) + 'm')
def vuat(v,u,a,t):
try:
if error_test(a,float) == False:
print('acceleration value is ' + str( ( float(v) - float(u) ) / float(t) ) + 'm/s**2')
elif error_test(v,float) == False:
print('final velocity value is ' + str( ( float(a) * float(t) ) + float(u) ) + 'm/s')
elif error_test(u,float) == False:
print('initial velocity value is ' + str( float(v) - ( float(a) * float(t) ) ) + 'm/s')
elif error_test(t,float) == False:
print('time value is ' + str( ( float(v) - float(u) )/ float(a) ) + 's')
except ZeroDivisionError:
print('You entered incorrect values. Division can\'t be completed')
except ValueError:
print('You need to enter 3 values. If you only have 2, you can\'t calculate acceleration.')
def error_test(x,y):
try:
y(x)
return True
except:
return False
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment