Here is how you can find which variables are None
missing_list = [ i for (i, z) in enumerate((v, u, a, t)) if z is None ]
This returns a sublist of [0, 1, 2, 3] with the indexes of the variables equal to None.
Gribouillis
Posting Maven
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11
Receive the variables as a tuple and then use each item as your variable.
def test_num_variables(*args):
print "number of variables =", args, type(args), len(args)
os = -1
if None in args:
os=args.index(None)
print "finding None =", os, args[os]
else:
print "None was not found"
return
## assumes that the check for numbers has already been done
total = 0
for idx in range(len(args)):
if idx != os:
total += args[idx]
print "adding test =", total
test_num_variables(1, 3, 5, None)
woooee
Posting Maven
2,707 posts since Dec 2006
Reputation Points: 827
Solved Threads: 780
Skill Endorsements: 9
With Exceptions:
from __future__ import division, print_function
def solve(v=None, u=None, a=None, t=None):
try:
v = u + a*t
print('v')
except TypeError:
try:
u = v - a*t
print('u')
except TypeError:
try:
a = (v-u) / t
print('a')
except TypeError:
print('t')
t = (v-u) / a
assert v == u + a*t
return v, u, a, t
print(solve(4, 2, 2))
print(solve(u=3, a=3.5, t=2))
pyTony
pyMod
6,312 posts since Apr 2010
Reputation Points: 879
Solved Threads: 987
Skill Endorsements: 26
A variation on woooee's idea
def F1(v, u, a, t):
args = (v, u, a, t)
if args.count(None) != 1:
raise TypeError("Exactly one argument of F1() must be None")
index = args.index(None)
Gribouillis
Posting Maven
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11
For completeness, you can also use a dictionary
def F1(*args):
if len(args) == 4:
to_dict ={}
to_dict["v"]=args[0]
to_dict["u"]=args[1]
to_dict["a"]=args[2]
to_dict["t"]=args[3]
for key in to_dict:
print key, to_dict[key] ## while testing
if to_dict[key] == None:
## etc
woooee
Posting Maven
2,707 posts since Dec 2006
Reputation Points: 827
Solved Threads: 780
Skill Endorsements: 9
Question Answered as of 1 Year Ago by
Gribouillis,
woooee,
snippsat
and 1 other