Takker for tilbakemeldinger.
Er nesten ferdig med den nå. I modulen: motionformulas er det bare en lang rekke med formler som jeg ikke er helt ferdig med.
Kode
from motionformulas import *
#If velocity is constant:
def fConst():
ans = 0
while ans < 1:
print ""
print "Enter 2 two values, leave the one that you want to find blank."
print ""
var = ['d','v','t'] # contains the variables,
value = ['','',''] # the values for d, v and t
names = ['Distance: (m) >', 'Velocity (m/s): >', 'Time (s): >']
for i in range(len(var)):
try:
value [i] = float(raw_input(names[i]))
except (ValueError): # If input is not a number,
print var[i], "is left empty." # variable will be left empty
empty = value.count('') # how many fields that was left empty
emptyvar = value.index('') # index of the empty variable
d = value[0]
v = value[1]
t = value[2]
# ans = 1 -> loop will not be repeated
print ""
if empty == 1:
if emptyvar == 0: # If d is empty -> findD()
findD(v,d)
ans = 1
elif emptyvar == 1: # If v is empty -> findV()
findV(d,t)
ans = 1
elif emptyvar == 2: # If t is empty -> findT()
findT(d,v)
ans = 1
else: print "Error: ..."
else:
print "Error: Too many fields were left empty. Try again"
print ""
print ""
print "Do you want to do another calculation?"
global new
new = input("Yes (1) or no (0)")
#If velocity is not constant:
def fNotConst():
names = ['Distance (m): >', 'Acceleration (m/s**2): >']
names += ['Final velocity (m/s): >', 'Initial velocity (m/s): >']
names += ['Time (s): >']
# Will find the object in a list
# and change the value the same index to 'find' in another list
def find(obj,search_list,change_list):
x = obj
y = search_list.index(x)
change_list[y] = 'find'
ans = 0
while ans!=1:
print ""
print "Distance: d"
print "Acceleration: a"
print "Final velocity: vf"
print "Initial velocity: vi"
print "Time: t"
print ""
x = str(raw_input("Which variable do you want to find? > "))
var = ['d','a', 'vf', 'vi', 't'] # contains the variables,
value = ['','','','',''] # the values for d, a, vf, vi and t
find(x, var, value) # Will make x equal 'find' in value
print "Enter the values that you know,"
print "at least 3 values are needed."
for i in range(len(var)):
if value[i] != 'find':
try:
value [i] = float(raw_input(names[i]))
except (ValueError): # If input is not a number,
print var[i], "is left empty." # variable will be left empty
empty = value.count('') # how many fields that was left empty
try:
emptyvar = value.index('') # index of the empty variable
except ValueError: pass
d = value[0]
a = value[1]
vf = value[2]
vi = value[3]
t = value[4]
print d,a,vf,vi,t
if empty <= 1:
if d == 'find':
print "skal finne d"
if a == '':
AFindD(vi,vf,t)
ans = 1
elif vf == '':
VfFindD(vi,t,a)
ans = 1
elif vi == '':
ViFindD(vf,t,a)
ans = 1
elif t == '':
TFindD()
ans = 1
elif a == 'find':
print "skal finne a"
if d == '':
DFindA(vf,vi,t)
ans = 1
elif vf == '':
VfFindA()
ans = 1
elif vi == '':
ViFindA()
ans = 1
elif t == '':
TFindA()
ans = 1
elif vf == 'find':
print "skal finne vf"
if a == '':
AFindVf()
ans = 1
elif d == '':
DFindVf(vi,a,t)
ans = 1
elif vi == '':
ViFindVf()
ans = 1
elif t == '':
TFindVf(vi,a,d)
ans = 1
elif vi == 'find':
print "skal finne vi"
if a == '':
AFindVi()
ans = 1
elif vf == '':
VfFindVi()
ans = 1
elif d == '':
DFindVi(vf,a,t)
ans = 1
elif t == '':
TFindVi()
ans = 1
elif t == 'find':
print "skal finne t"
if a == '':
AFindT()
ans = 1
elif vf == '':
VfFindT()
ans = 1
elif vi == '':
ViFindT()
ans = 1
elif D == '':
DFindT(vf,vi,a)
ans = 1
else: print "Error: No variable was selected to be found, try again"
else:
print "Error: Too many fields were left empty"
print " or written in one format, try again"
print ""
print "Do you want to do another calculation?"
global new
new = input("Yes (1) or no (0)")
new = 1
while new == 1:
print "Is the velocity constant?"
const = input("Yes (1) or no (0) > ")
if const == 1:
fConst()
elif const == 0:
fNotConst()
else:
print "ERROR"